Вопрос

допустим, у меня есть данный 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: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>

Другие советы

Вы можете сделать это, используя xsl:sort.Важно установить data-type="число", потому что в противном случае позиция будет отсортирована как строка, поэтому 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[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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top