[Previous] [Contents] [Next]


Sorting


Sorting is specified by adding xsl:sort elements as children of xsl:apply-templates or xsl:for-each elements. The first xsl:sort child specifies the primary sort key, the second xsl:sort child specifies the secondary sort key and so on.

When an xsl:apply-templates or xsl:for-each element has one or more xsl:sort children, then instead of processing the selected elements in the order they appear in the XML document order, the XML processor sorts the elements according to the specified sort keys and processes them in the sorted order.

When used in an xsl:for-each element, xsl:sort elements must occur first.

An xsl:sort element has a select attribute with a value that is a select pattern. For each element to be processed, the select pattern is evaluated with that element as the current element. The value of the first selected element is used as the sort key for that element. The default value of the select attribute is '.' (the current element).



The select string serves as the sort key. The following optional attributes on the xsl:sort element control how the list of sort keys is sorted:

order-Specifies whether the strings should be sorted in ascending or descending order; ascending specifies ascending order; descending specifies descending order; the default is ascending.
lang-Specifies the language of the sort keys; it has the same range of values as the xml:lang attribute; if no lang value is specified, the language is determined from the system environment.
data-type-Specifies the data type of the sort strings; the following values are allowed:
text-Specifies that the sort keys should be sorted alphabetically in the correct manner for the language specified by lang
number-Specifies that the sort keys are to be converted into numbers and then sorted according to their numeric values; the value specified by lang can be used to help convert the values into numbers. The default value is text.
case-order-This can have the value upper-first or lower-first. This value applies when data-type="text", and specifies that the uppercase characters should be sorted before the lowercase letters, or vice versa respectively. For example, if lang="en" then A B a A becomes A a B b when sorted with case-order="upper-first". It becomes a A b B when sorted with case-order="lower-first". The default value depends on the language specified.

For example, suppose you have a CD database marked up in XML:

<CDs>
     <CD>
       <artist>Tori Amos</artist>
       <title>Boys for Pele</title>
     </CD>
</CDs>

You can then generate a list of the CDs sorted by artist using the XSL code shown in Listing 20.17.

Listing 20.17 Using XSL to Sort Elements


1:  <xsl:template match="CDs">
2:    <ul>
3:      <xsl:apply-templates select="CD">
4:        <xsl:sort select="artist"/>
5:        <xsl:sort select="title"/>
6:      </xsl:apply-templates>
7:    </ul>
8:  </xsl:template>
9:
10: <xsl:template match="CD">
11:   <li>
12:     <xsl:value-of select="artist"/>
13:       <xsl:text> </xsl:text>
14:     <xsl:value-of select="title"/>
15:   </li>
16: </xsl:template>


[Previous] [Contents] [Next]