SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Notices
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
XPath API for Java
Saxon on .NET
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
| Introduction
| xsl:analyze-string
| xsl:apply-imports
| xsl:apply-templates
| xsl:attribute
| xsl:attribute-set
| xsl:call-template
| xsl:character-map
| xsl:choose
| xsl:comment
| xsl:copy
| xsl:copy-of
| xsl:decimal-format
| xsl:document
| xsl:element
| xsl:fallback
| xsl:for-each
| xsl:for-each-group
| xsl:function
| xsl:if
| xsl:include
| xsl:import
| xsl:import-schema
| xsl:key
| xsl:matching-substring
| xsl:message
| xsl:namespace
| xsl:namespace-alias
| xsl:next-match
| xsl:non-matching-substring
| xsl:number
| xsl:otherwise
| xsl:output
| xsl:output-character
| xsl:param
| xsl:perform-sort
| xsl:preserve-space
| xsl:processing-instruction
| xsl:result-document
| xsl:sequence
» | xsl:sort
| xsl:strip-space
| xsl:stylesheet
| xsl:template
| xsl:text
| xsl:value-of
| xsl:variable
| xsl:when
| xsl:with-param
| Literal Result Elements
| XSLT Patterns
XPath 2.0 Expression Syntax
Function Library
Standards Conformance

# xsl:sort

The `xsl:sort` element is used within an `xsl:for-each` or `xsl:apply-templates` or `saxon:group` element to indicate the order in which the selected elements are processed.

The `select` attribute (default value ".") is a string expression that calculates the sort key.

The `order` attribute (values "ascending" or "descending", default "ascending") determines the sort order. There is no control over language, collating sequence, or data type.

The `data-type` 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 `xs:date` or `xs:decimal`.

The `collation` attribute is the name of a collating sequence. If present it must refer to a name established using the saxon:collation declaration in the stylesheet.

The `case-order` 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.

The value of the `lang` 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 only collating sequence supplied with the Saxon product is "en" (English), but other values may be supported by writing a user-defined comparison class. If no comparison class is found for the specified language, a default algorithm is used which simply sorts according to Unicode binary character codes. The value of lang does not have to be a recognized language code, it is also possible to use values such as "month" to select a data-type-specific collating algorithm.

Several sort keys are allowed: they are written in major-to-minor order.

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.

    <xsl:template match="BOOKLIST">
        <h2>
            <xsl:apply-templates select="BOOK">
                <xsl:sort select="AUTHOR"/>
                <xsl:sort select="@DATE" order="descending" lang="GregorianDate"/>
            </xsl:apply-templates>
        </h2>
    </xsl:template>

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.

    <xsl:template match="BOOKLIST">
        <h2>
            <xsl:for-each select="BOOK">
                <xsl:sort select="AUTHOR"/>
                <p>AUTHOR: <xsl:value-of select="AUTHOR"></p>
                <p>TITLE: <xsl:value-of select="TITLE"></p>
                <hr/>
            </xsl:for-each>
        </h2>
    </xsl:template>

Next