ELEMENT:  xsl:sort

<xsl:sort
  case-order="upper-first" | "lower-first"
  data-type="number" "qname" | "text"
  lang="language-code"
  order="ascending" | " descending"
  select="expression"
>
</xsl:sort>

 
The xsl:sort element is used to define a sort key. This sort key determines the order in which selected nodes are processed by the xsl:for-each or xsl:apply-templates elements.
 
A sort can be based upon more than one xsl:sort element. Each sort is applied in the order in which it occurs. Duplicate values are left in document order. After the first sort has reordered the nodes, the second sort is applied to any nodes that had duplicate values in the first sort. The third sort is applied to any nodes that had duplicate values in the second sort, and so on.
 
This is not a self-closing element. The separate closing element is mandatory.
 
case-order="upper-first" | "lower-first"
 
The optional case-order attribute dictates whether the sort will have upper or lower case letters listed first in the sort. The default is to list upper case first.
 
data-type="number" | "qname" | "text"
 
The optional data-type attribute specifies the data type of the strings. There are only three permitted types:
ValueEffect
numberThe sort key is converted to a number.
qnameThe sort is based upon a user-defined data type.
textThe sort is alphabetic.

 
lang="language-code"
 
The optional lang attribute is set to an Attribute Value Template or a string that dictates the language code which in turn specifies the language alphabet to be used for the sort. Clearly, the alphabet and numbers being used will determine the sort order. The default language is set by the operating system environment.
 
order="ascending" | " descending"
 
The optional order attribute dictates whether the sort is in increasing or decreasing order. The default is ascending.
 
select="expression"
 
The optional select attribute is an expression that defines the key upon which the sort will be based. The expression is evaluated and converted to a string that is used as the sort key. If no select attribute is provided (hence, no sort key), the selected nodes are sorted in document order.
 
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_sort.xsl"?>
and we name it: xslt_example_sort.xml
 
Code for xslt_example_sort.xsl:
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:sort select="age" order="descending" />
<xsl:value-of select="name" />
<xsl:text> - </xsl:text>
<xsl:value-of select="age" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 
Output:
 
Click to view output in separate window - requires Internet Explorer

 
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information