Attributes The Details on the xsl:sort Element
page 110 xsl:value-of select=newline
xsl:for-each xsl:template
xsl:stylesheet
When we sort these elements using
data-type=text
, heres what we get:
10 127
23
We get this result because a text-based sort puts anything that starts with a 1 before anything that starts with a 2. If we change the
xsl:sort
element to be
xsl:sort select=. data-type=number
, we get these results:
10 27
123
If you use something else here
data-type=floating-point
, for example, what the XSLT processor does is anybodys guess. The XSLT specification allows for other
values here, but its up to the XSLT processor to decide how or if it wants to process those values. Check your processors documentation to see if it does anything relevant
or useful for values other than
data-type=text
or
data-type=number
. A final note: if youre using
data-type=number
, and any of the values arent numbers, those non-numeric values will sort before the numeric values. That means if
youre using
order=ascending
, the non-numeric values appear first; if you use
order=descending
, the non-numeric values appear last.
?xml version=1.0? numberlist
number127number number23number
numberzzznumber number10number
numberyyynumber numberlist
Given this less-than-perfect data, here are the correctly sorted results:
zzz yyy
10 23
127
Notice that the non-numeric values were not sorted; they simply appear in the output document in the order in which they were encountered.
order
You can order the sort as
order=ascending
or
order=descending
. The default is
order=ascending
.
case-order
This attribute can have two values.
case-order=upper-first
means that uppercase letters sort before lowercase letters, and
case-order=lower-first
means that lowercase letters sort first. The
case-order
attribute is used only when the
data-type
attribute is
text
. The default value depends on the value of the soon-to-be-discussed
lang
attribute.
page 111 lang
This attribute defines the language of the sort keys. The valid values for this attribute are the same as those for the
xml:lang
attribute defined in Section 2.12 of the XML 1.0 specification. The language codes are those commonly used in Java programming,
UNIX locales, and other places ISO language and country namings are defined. For example,
lang=en
means English,
lang=en-US
means U.S. English, and
lang=en-GB
means U.K. English. Without the
lang
attribute its rarely used in practice, the XSLT processor determines the default language from the system
environment.
6.1.2.3 Where can you use xsl:sort?
The
xsl:sort
element can appear inside two elements:
•
xsl:apply-templates
•
xsl:for-each
If you use an
xsl:sort
element inside
xsl:for-each
, the
xsl:sort
elements must appear first. If you tried something like this, youd get an exception from the XSLT
processor:
xsl:for-each select=addressbookaddress xsl:sort select=namelast-name
xsl:value-of select=nametitle xsl:sort select=namefirst-name -- NOT LEGAL --
...