【问题标题】:Cover text() node of existing node from new node - XSLT从新节点覆盖现有节点的文本()节点 - XSLT
【发布时间】: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元素名称,并且应该用&lt;para&gt;元素覆盖文本内容。

所以我的输出应该是,

<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 包含嵌套列表的文本没有被&lt;para&gt; 元素正确覆盖。

在上面的示例中,我为嵌套列表获取的当前输出如下,

<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>

我试图用&lt;para&gt; 覆盖li 元素的text() 内容,但随后&lt;span&gt; 元素没有出现在输出中。

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

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    我会简单地更改模板:

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

    到:

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

    此模板将匹配包含&lt;p&gt;&lt;li&gt;。在这种情况下,您不必添加一些&lt;para&gt;。对于包含一些&lt;p&gt;&lt;li&gt;s,另一个模板将匹配,并输出所需的&lt;para&gt;

    或者,你也可以这样做:

    • 丢弃模板&lt;xsl:template match="li/p"&gt;
    • 修改模板&lt;xsl:template match="li"&gt;如下:

      <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>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2022-11-11
      • 1970-01-01
      • 2017-05-04
      • 2021-09-05
      • 2017-05-08
      相关资源
      最近更新 更多