从新节点覆盖现有节点的 text() 节点 - XSLT

Cover text() node of existing node from new node - XSLT

我正在进行 html 到 xml 的转换。在 html 我有 table 这样的,

<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> 元素正确覆盖。

在上面的示例中,我为嵌套列表获得的当前输出是 follwos,

<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> 元素没有出现在输出中。

任何人都可以建议一种方法,我该怎么做..

我只想更改模板:

<xsl:template match="li/p">
    <xsl:apply-templates/>
</xsl:template>

至:

<xsl:template match="li[p]">
   <listitem>
     <xsl:apply-templates/>
   </listitem>
</xsl:template>

此模板将匹配包含 <p><li>。在这种情况下,您不必添加一些 <para>。对于包含一些 <p><li>,另一个模板将匹配,并且它确实输出所需的 <para>

或者,你也可以这样做:

  • 丢弃模板 <xsl:template match="li/p">
  • 修改模板<xsl:template match="li">如下:

    <xsl:template match="li">
      <xsl:choose>
        <xsl:when test="p">
          <listitem>
            <xsl:apply-templates/>
          </listitem>
        </xsl:when>
        <xsl:otherwise>
          <listitem>
            <para>
              <xsl:apply-templates/>
            </para>
          </listitem>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>