【发布时间】:2015-10-05 04:27:58
【问题描述】:
我正在做 html 到 xml 的转换。在html中我有这样的表格,
<doc>
<ol>
<li>
<p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
<ol>
<li>nested list1</li>
<li>nested <span style="color: rgb(255,0,255);">The scope of this project is to:</span> list1</li>
<li>nested list1</li>
<li>nested list1</li>
<li>nested list1</li>
</ol>
</li>
<li>
<p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
</li>
<li>
<p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
</li>
</ol>
这里我需要将html元素名称转换为某些xml元素名称,并且应该用<para>元素覆盖文本内容。
所以我的输出应该是,
<doc>
<oli>
<listitem><para>List<style type="color: rgb(255,0,255);">The scope of this project is to:</style></para>
<oli>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested <style type="color: rgb(255,0,255);">The scope of this project is to:</style> list1</para></listitem>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested list1</para></listitem>
</oli>
</listitem>
<listitem><para>
List<style type="color: rgb(255,0,255);">The scope of this project is to:</style>
</para></listitem>
<listitem><para>
List<style type="color: rgb(255,0,255);">The scope of this project is to:</style>
</para></listitem>
</oli>
</doc>
我已经编写了以下 xsl 来将 html 转换为 xml,
<xsl:template match="ol">
<oli>
<xsl:apply-templates/>
</oli>
</xsl:template>
<xsl:template match="li">
<listitem>
<para>
<xsl:apply-templates/>
</para>
</listitem>
</xsl:template>
<xsl:template match="span">
<style type="{@style}">
<xsl:apply-templates/>
</style>
</xsl:template>
<xsl:template match="p">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="li/p">
<xsl:apply-templates/>
</xsl:template>
我遇到的问题是当ul 包含嵌套列表时,li 包含嵌套列表的文本没有被<para> 元素正确覆盖。
在上面的示例中,我为嵌套列表获取的当前输出如下,
<listitem><para>
List<style type="color: rgb(255,0,255);">The scope of this project is to:</style>
<oli>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested <style type="color: rgb(255,0,255);">The scope of this project is to:</style> list1</para></listitem>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested list1</para></listitem>
<listitem><para>nested list1</para></listitem>
</oli>
</para></listitem>
如此简单,而不是
<listitem><para>
List<style type="color: rgb(255,0,255);">The scope of this project is to:</style>
<oli>
........
</oli>
</para>
</listitem>
我需要,
<listitem><para>
List<style type="color: rgb(255,0,255);">The scope of this project is to:</style>
</para>
<oli>
........
</oli>
</listitem>
我试图用<para> 覆盖li 元素的text() 内容,但随后<span> 元素没有出现在输出中。
任何人都可以建议一种方法我该怎么做..
【问题讨论】: