我有一个项目清单:
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
我想要作为输出
z
c
x
a
我的文件中没有订单信息,我只想反转这些行。源文件中的最后一行应该是输出中的第一行。如何使用 XSLT 解决此问题,而不按项目内容排序,这会给出错误的结果?
我将介绍两个 XSLT 解决方案:
I. 带递归的 XSLT 1.0 请注意,此解决方案适用于任何节点集,而不仅仅是在节点是兄弟节点的情况下:
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:call-template name="reverse">
<xsl:with-param name="pList" select="*"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pList"/>
<xsl:if test="$pList">
<xsl:value-of
select="concat($pList[last()], '
')"/>
<xsl:call-template name="reverse">
<xsl:with-param name="pList"
select="$pList[not(position() = last())]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当应用于此 XML 文档时:
<t>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</t>
产生想要的结果:
z
c
x
a
二. XSLT 2.0 解决方案 :
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:value-of select="reverse(*)/string(.)"
separator="
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于同一个 XML 文档时, 产生相同的正确结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)