以相反的顺序应用模板

时间:2020-03-05 18:49:23  来源:igfitidea点击:

说我有这个给定的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>

解决方案

回答

我们可以使用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>

回答

简单!

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