且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XSLT:如何在不按内容排序的情况下反转输出

更新时间:2022-06-08 22:22:14

XML CODE:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<device>
<element>a</element>
<element>x</element>
<element>c</element>
<element>z</element>
</device>

XSLT 代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//device">
<xsl:for-each select="element">

<xsl:sort select="position()" data-type="number" order="descending"/>

<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>

注意:如果您使用的是 data-type="number",并且任何值都不是数字,那么这些非数字值将排在数字值之前.这意味着如果您使用 order="ascending",则首先出现非数字值;如果使用 order="descending",则非数字值最后出现.

note: if you're using data-type="number", and any of the values aren't numbers, those non-numeric values will sort before the numeric values. That means if you're using order="ascending", the non-numeric values appear first; if you use order="descending", the non-numeric values appear last.

注意非数字值未排序;它们只是按照它们出现的顺序出现在输出文档中.

Notice that the non-numeric values were not sorted; they simply appear in the output document in the order in which they were encountered.

此外,您可能会发现阅读此内容很有用:

also, you may find usefull to read this:

http://docstore.mik.ua/orelly/xml/xslt/ch06_01.htm