Sorting XML Using <xsl:sort>

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Sorting XML Using <xsl:sort>

You can use the <xsl:sort> instruction to sort XML elements so that they are processed (and so that the results are output) in a specific order. Sorting is applicable to a template rule (<xsl:template>) or a loop (<xsl:for-each>). So <xsl:sort> elements are declared as children of an <xsl:apply-templates> element or an <xsl:for-each> element.

The <xsl:sort> element has a select attribute whose value is an XPath expression. This attribute defines a sort key. Multiple <xsl:sort> elements are allowed for each template rule or each loop. The first <xsl:sort> element defines the primary sort key, the second <xsl:sort> element specifies the secondary sort key, and so on. In the example below, employees are sorted by their last name. Employees who share the same last name are further sorted by their first name. When two sort keys are identical, the resulting order is the same as the order they appear in the source document.

When used in <xsl:for-each>elements, <xsl:sort> elements must occur before any other child elements.

You can sort items in either ascending order or descending order. You can specify the sort order by assigning the appropriate string value ("ascending" or "descending") to the order attribute of the <xsl:sort> element. The default order is "ascending".

There are three types of sort keys: QNames, text, and numbers. QNames and text are sorted alphabetically. Numbers are sorted by their numerical values. The default data type is text. To specify a data type explicitly, assign "QName", "text", or "number" to the data-type attribute of the <xsl:sort> element When data-type is set to "number", the sort key is first converted to a number. A qualified name with a prefix is expanded into an expanded name. The expanded name identifies the data type.

For more information about sorting, see Sorting with XSLT.

The following example sorts a list of employees by name.

Example

This example uses an XML source file and an XSLT style sheet.

XML File (sortempl.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="sortempl.xsl"?>
<employees>
  <employee>
    <name>
      <given>Loni</given>
      <family>Ota</family>
    </name>
  </employee>
  <employee>
    <name>
      <given>Leonard</given>
      <family>Zuvela</family>
    </name>
  </employee>
  <employee>
    <name>
      <given>John</given>
      <family>Chen</family>
    </name>
  </employee>
  <employee>
    <name>
      <given>Jay</given>
      <family>Dirkson</family>
    </name>
  </employee>
  <employee>
    <name>
      <given>Anthony</given>
      <family>Dirkson</family>
    </name>
  </employee>
  <employee>
    <name>
      <given>Tony</given>
      <family>Dirkson</family>
    </name>
  </employee>
</employees>

XSLT File (sortempl.xsl)

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<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>

</xsl:stylesheet>

Formatted Output

  • John Chen
  • Anthony Dirkson
  • Jay Dirkson
  • Tony Dirkson
  • Loni Ota
  • Leonard Zuvela

See Also

Qualified Names | Expressions