【问题标题】:apply-templates in reverse order以相反的顺序应用模板
【发布时间】:2010-09-08 04:06:51
【问题描述】:

假设我有这个给定的 XML 文件:

<root>
    <node>x</node>
    <node>y</node>
    <node>a</node>
</root>

我希望显示以下内容:

ayx

使用类似于:

<xsl:template match="/">
    <xsl:apply-templates select="root/node"/>
</xsl:template>
<xsl:template match="node">
    <xsl:value-of select="."/>
</xsl:template>

【问题讨论】:

    标签: xslt sorting


    【解决方案1】:

    简单!

    <xsl:template match="/">
        <xsl:apply-templates select="root/node">
            <xsl:sort select="position()" data-type="number" order="descending"/>
        </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="node">
        <xsl:value-of select="."/>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      您可以使用xsl:sort 执行此操作。设置data-type="number" 很重要,否则,位置将被排序为字符串,因此,第 10 个节点将在第 2 个节点之前考虑。

      <xsl:template match="/">
          <xsl:apply-templates select="root/node">
              <xsl:sort 
                  select="position()" 
                  order="descending" 
                  data-type="number"/>
          </xsl:apply-templates>
      </xsl:template>
      <xsl:template match="node">
          <xsl:value-of select="."/>
      </xsl:template>
      

      【讨论】:

        【解决方案3】:
        <xsl:template match="/">
                <xsl:apply-templates select="root/node[3]"/>
                <xsl:apply-templates select="root/node[2]"/>
                <xsl:apply-templates select="root/node[1]"/>
            </xsl:template>
            <xsl:template match="node">
                <xsl:value-of select="."/>
            </xsl:template>
        

        【讨论】:

        • 第四个节点来了怎么办??您的逻辑将只处理 3 个节点,其余所有节点都将被删除...
        猜你喜欢
        • 2012-02-05
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2016-09-20
        • 2015-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多