Click to return to the XML (Extensible Markup Language) home page    
Accessing and Outputting ...     Conditional Templates     Getting Started with XSL    
Web Workshop  |  XML (Extensible Markup Language)

Sorting XML


When using the <xsl:for-each> element to display a repetitive data structure, the template within the <xsl:for-each> is normally processed for each selected item in the order that they appear in the source document. The order-by attribute allows you to sort the selected items before iterating over the templates.

The syntax of the order-by attribute is an XSL Pattern that points to a node in the source document that should be used as the sort key.

Given the following XML data (same as that used in Creating and Populating an HTML Template):

<?xml version="1.0"?>
<portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <stock exchange="nyse">
    <name>zacx corp</name>
    <symbol>ZCXM</symbol>
    <price dt:dt="number">28.875</price>
  </stock>
  <stock exchange="nasdaq">
    <name>zaffymat inc</name>
    <symbol>ZFFX</symbol>
    <price dt:dt="number">92.250</price>
  </stock>
  <stock exchange="nasdaq">
    <name>zysmergy inc</name>
    <symbol>ZYSZ</symbol>
    <price dt:dt="number">20.313</price>
  </stock>
</portfolio>

An order-by="price" can be added to the <xsl:for-each> element to sort the stocks by price.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
    <HTML>
      <BODY>
        <TABLE BORDER="2">
          <TR>
            <TD>Symbol</TD>
            <TD>Name</TD>
            <TD>Price</TD>
          </TR>
          <xsl:for-each select="portfolio/stock" order-by="price">
            <TR>
              <TD><xsl:value-of select="symbol"/></TD>
              <TD><xsl:value-of select="name"/></TD>
              <TD><xsl:value-of select="price"/></TD>
            </TR>
          </xsl:for-each>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

The value of the node pointed to by the order-by pattern is sorted according to its data type. In this case the data type is declared to be a number by the dt:dt attribute, so the sorting will be done numerically. Non-data-typed values will be compared as strings.

A sort key can be preceded by a minus sign (-) to indicate that the sort should be in descending order. Likewise, a plus sign (+) can optionally be used to indicate explicitly that the sort is in ascending order (the default).

The order-by is also available on the <xsl:apply-templates> element.

Try it! You can view the sample above by clicking Portfolio Sample (Ascending Sort) or Portfolio Sample (Descending Sort) (order-by="-price").

Download Download these samples.

Note The current W3C XSL Working Draft describes a different sorting syntax, using an <xsl:sort> element. Microsoft plans to adopt this syntax in future releases.

More on Sort Keys

Multiple sort keys are specified by separating them by semicolons. If the first sort key does not unambiguously order an item, the next key is tested to resolve the conflict. Here is an example of a more complex sort key.

order-by="BIDDER; - number(PRICE)"

The number and date methods can be used to obtain data-typed sorts when the source document has no data type information declared. The example above casts the value of the PRICE element as a number for purposes of sorting.

Try it! The above sort key is used by the "sort-bidder-price-.xsl" style sheet in the XML/XSL Viewer Sample.

Download Download this sample.



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.