only for RuBoard |
xsl:sort specifies sort criteria for a node list selected by xsl:value-of or
xsl:apply-templates .
Attribute Name | Enforced | Description | Values |
---|---|---|---|
select | Optional | The sort key for the node. If not specified, uses the value of the current node ( . ) as the sort key. | A string value, usually an XPath expression. |
data-type | Optional | Specifies the data type for the strings. | text , number , or a QName that's expanded into a dt-expanded-name . The expanded-name identifies the data type. |
order | Optional | Specifies the sort order. | ascending or descending |
lang | Optional | Specifies which language's alphabet is used to determine sort order. | NMToken . If unspecified, the language is determined from the system's environment. |
case-order | Optional | Determines if strings will be sorted with uppercase first or lowercase first. | upper-first or lower-first . The default is lower- first (despite the documentation in MSDN to the contrary). |
Consider the following XML document, test.xml :
<?xml version="1.0" encoding="utf-8" ?> <links> <link name="C" id="1" /> <link name="B" id="2" /> <link name="A" id="11" /> <link name="b" id="22" /> <link name="a" id="12" /> <link name="c" id="23" /> </links>
The following stylesheet examples have a single template rule that matches the root node. They iterate through the link elements, which are sorted. The only difference is their xsl:sort element's parameters and the output generated:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" omit-xml-declaration="yes" /> <xsl:template match="/"> <xsl:for-each select="links/link"> <xsl:sort select="attribute::name" order="descending" case-order="lower-first" /> <xsl:value-of select="@name" /> <xsl:if test="position() != last()"> <xsl:text>,</xsl:text> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The preceding stylesheet yields the following:
C,c,B,b,A,a
Notice that we specified the case-order attribute as lower-first . The uppercase letters are rendered first in the example. This is because the document is first sorted using the case-order and then the sort order is applied. To see this more clearly, sort in ascending order and change the previous stylesheet rule's xsl:sort element to the following declaration:
<xsl:sort select="attribute::name" case-order="upper-first" />
This yields the following:
A,a,B,b,C,c
Changing the line of text to use all the defaults looks like this:
<xsl:sort select="attribute::name" />
The default behavior yields the following.
A,a,B,b,C,c
The preceding examples are sorted based on text. The following examples show sorting with numeric data. We are now sorting based on the id attribute:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" omit-xml-declaration="yes" /> <xsl:template match="/"> <xsl:for-each select="links/link"> <xsl:sort select="attribute::id" /> <xsl:value-of select="@id" /> <xsl:if test="position() != last()"> <xsl:text>,</xsl:text> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
This stylesheet yields the following:
1,11,12,2,22,23
Instead of using the default data type, text, you can specify the data-type attribute as number :
<xsl:sort select="attribute::id" data-type="number"/>
This yields a numeric sorting instead of character sorting:
1,2,11,12,22,23
xsl:apply-templates, xsl:for-each
None.
only for RuBoard |