【问题标题】:How to parse nested tags using XSLT in sequence?如何按顺序使用 XSLT 解析嵌套标签?
【发布时间】:2011-10-14 05:52:22
【问题描述】:

我的 XML 有以下场景。

<content>
  <para>text-1 <emphasis type="bold">text-2</emphasis> text-3</para>
</content>

我想像下面这样解析它

<content>
<p>text-1 <b>text-2</b> text-3</p>
</content>

我已经创建了如下的 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="ISO-8859-1" indent="no"/>

<xsl:template name="para">
    <p>
        <xsl:value-of select="text()" disable-output-escaping="yes"/>
        <xsl:for-each select="child::*">
            <xsl:if test="name()='emphasis'">
                <xsl:call-template name="emphasis"/>
            </xsl:if>
        </xsl:for-each>

    </p>
</xsl:template>
<xsl:template name="emphasis">
    <xsl:if test="attribute::type = 'bold'">
        <b>
            <xsl:value-of select="text()" disable-output-escaping="yes"/>
        </b>
    </xsl:if>
</xsl:template>

<xsl:template match="/">
    <content>
        <xsl:for-each select="content/child::*">
            <xsl:if test="name()='para'">
                <xsl:call-template name="para"/>
            </xsl:if>
        </xsl:for-each>
    </content>
</xsl:template>
</xsl:stylesheet>

上面提供的 XSLT 正在生成如下所示的输出

<content>
 <p>text-1  text-3<b>text-2 </b></p>
</content>

请指导我您的建议,我怎样才能得到我的愿望输出?

【问题讨论】:

    标签: xslt nested transformation


    【解决方案1】:

    为此,您只需使用特殊情况扩展标准身份转换,以匹配您的 paraemphasis 元素。例如,对于 para 元素,您可以如下将 para 替换为 p,然后继续匹配所有子节点

    <xsl:template match="para">
        <p>
            <xsl:apply-templates select="@*|node()"/>
        </p>
    </xsl:template>
    

    所以,给定以下 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" />
    
        <!-- This is the Identity Transform -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- Replace para with p -->
        <xsl:template match="para">
            <p>
                <xsl:apply-templates select="@*|node()"/>
            </p>
        </xsl:template>
    
        <!-- Replace emphasis with b -->
        <xsl:template match="emphasis[@type='bold']">
            <b>
                <xsl:apply-templates select="node()"/>
            </b>
        </xsl:template>
    </xsl:stylesheet>
    

    应用于以下输入 XML 时

    <content> 
      <para>text-1 <emphasis type="bold">text-2</emphasis> text-3</para> 
    </content> 
    

    以下是输出

    <content>
        <p>text-1 <b>text-2</b> text-3</p>
    </content>
    

    如果您输入的 XML 有更多要转换的标签,您应该能够看到扩展到其他情况是多么容易。

    【讨论】:

      【解决方案2】:

      这样做;)

      <?xml version="1.0" encoding="utf-8" ?>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      >
      
          <xsl:template match="content">
              <content><xsl:apply-templates select="para" /></content>
          </xsl:template>
      
          <xsl:template match="emphasis [@type='bold']">
              <b><xsl:value-of select="." /></b>
          </xsl:template>
      
      </xsl:stylesheet>
      

      当你这样做时,默认模板将捕获 text-1 和 text-3

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 2013-06-17
        • 1970-01-01
        • 2014-02-22
        • 2012-05-26
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        相关资源
        最近更新 更多