Применять шаблоны в обратном порядке
скажем, у меня есть данный 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>
Ответы
Ответ 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. Важно установить тип данных = "число", потому что иначе позиция будет сортироваться как строка, в конце концов, 10-й node будет рассмотрен до 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>