<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="xsl-elements" page="sort" subpage=""/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: xsl:sort</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:sort"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:sort</h1>
      <p>The <code>xsl:sort</code> element is used within an <code>xsl:for-each</code> or <code>xsl:apply-templates</code>
or <code>saxon:group</code> element to indicate the order in which the selected elements are processed.</p>
      <p>The <code>select</code> attribute (default value ".") is a string expression that calculates the sort
key.</p>
      <p>The <code>order</code> attribute (values "ascending" or "descending", default "ascending") determines
the sort order. There is no control over language, collating sequence, or data type.</p>
      <p>The <code>data-type</code> attribute determines whether collating is based
on alphabetic sequence or numeric sequence. The permitted values are either "text" or "number", or
a built-in type in XML Schema, such as <code>xs:date</code> or <code>xs:decimal</code>.</p>
      <p>The <code>collation</code> attribute is the name of a collating sequence. If present it must be a collation URI
recognized by Saxon: see <a class="bodylink" href="../extensibility/collation.xml">Implementing a collating sequence</a>.</p>
      <p>The <code>case-order</code> attribute (values "upper-first" and "lower-first") is relevant only for
data-type="text"; it determines whether uppercase letters are sorted before their lowercase equivalents,
or vice-versa.</p>
      <p>The value of the <code>lang</code> attribute can be an ISO language code such as "en" (English) or
 "de" (German). It determines the algorithm used for alphabetic collating. The default is based on
 the Java system locale. The value is used to select a collating sequence associated with the Java Locale
 for that language.</p>
      <p>Several sort keys are allowed: they are written in major-to-minor order.</p>
      <p>Example 1: sorting with xsl:apply-templates. This example shows a template for a BOOKLIST
element which processes all the child BOOK elements in order of their child AUTHOR elements; books
with the same author are in descending order of the DATE attribute.</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:template match="BOOKLIST"&gt;
    &lt;h2&gt;
        &lt;xsl:apply-templates select="BOOK"&gt;
            &lt;xsl:sort select="AUTHOR"/&gt;
            &lt;xsl:sort select="@DATE" order="descending" lang="GregorianDate"/&gt;
        &lt;/xsl:apply-templates&gt;            
    &lt;/h2&gt;
&lt;/xsl:template&gt;</code>
         </pre>
      </div>
      <p>Example 2: sorting with xsl:for-each. This example also shows a template for a BOOKLIST
element which processes all the child BOOK elements in order of their child AUTHOR elements.</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:template match="BOOKLIST"&gt;
    &lt;h2&gt;
        &lt;xsl:for-each select="BOOK"&gt;
            &lt;xsl:sort select="AUTHOR"/&gt;
            &lt;p&gt;AUTHOR: &lt;xsl:value-of select="AUTHOR"&gt;&lt;/p&gt;
            &lt;p&gt;TITLE: &lt;xsl:value-of select="TITLE"&gt;&lt;/p&gt;
            &lt;hr/&gt;
        &lt;/xsl:for-each&gt;            
    &lt;/h2&gt;
&lt;/xsl:template&gt;</code>
         </pre>
      </div>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="strip-space.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>