Microsoft XML SDK 2.6 - XSLT Reference

xsl:sort Element

Specifies sort criteria for node lists selected by xsl:for-each or xsl:apply-templates.

<xsl:sort
  select = string-expression 
  data-type = { "text" | "number" | Qname }
  order = { "ascending" | "descending" }
  /> 

Attributes

select
An expression that is evaluated with that node as the current node and with the complete list of nodes being processed in unsorted order as the current node-list. The resulting object is converted to a string that is used as the sort key for that node. The default value of the select attribute is ".", which will cause the string-value of the current node to be used as the sort key.
data-type
Specifies the data type of the strings; the following values are allowed:
"text" Specifies that the sort keys should be sorted alphabetically.
number Specifies that the sort keys should be converted to numbers and then sorted according to the numeric value. The sort key is converted to a number.
QName Expanded into a dt-expanded-name. The expanded-name identifies the data type.

If no data type is specified, the type of the expression will be used as the default.

order
Specifies whether the strings will be sorted in ascending or descending order. The default is ascending.

Element Information

Number of occurrences Unlimited
Parent elements xsl:apply-templates, xsl:for-each
Child elements (No child elements)

Remarks

Inherent data types (from XML Schema) are supported in the following manner:

Example

For example, suppose an employee database has the form

<employees>
  <employee>
    <name>
      <given>James</given>
      <family>Clark</family>
    </name>
    ...
  </employee>
</employees>

Then a list of employees sorted by name could be generated using:

<xsl:template match="employees">
  <ul>
    <xsl:apply-templates select="employee">
      <xsl:sort select="name/family"/>
      <xsl:sort select="name/given"/>
    </xsl:apply-templates>
  </ul>
</xsl:template>
<xsl:template match="employee">
  <li>
    <xsl:value-of select="name/given"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="name/family"/>
  </li>
</xsl:template>

See Also

Sorting XML