Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Another way of making the sort conditional is to use a conditional expression as the sort key. This is much easier in XSLT 2.0 with the introduction of conditional expressions in XPath:
else if ($sort-key = ‘author’) then author
else if ($sort-key = ‘isbn’) then isbn
else publisher”>
If the computation of the sort key is really complicated, you can do it in a sequence constructor rather than in the
select
attribute. This can even invoke templates or build temporary trees—there are no limits.
There are two other solutions to this problem that are worth mentioning, although both have their disadvantages:
Examples
I'll start with a couple of simple examples and then show a full working example that you can download and try yourself.
If
select=“.”
were omitted from the
Example: Sorting on the Result of a Calculation
This example outputs a list of products, sorted by the total sales of each product, in descending order.
Source
This is the file
products.xml
:
Stylesheet
products.xsl
is a complete stylesheet written using the simplified stylesheet syntax, in which the entire stylesheet module is written as a single literal result element. Simplified stylesheets are described in Chapter 3, on page 125.
The
sales
attribute over all their
sum()
function (described on page 889) and displayed using the
format-number()
function (page 788).
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
sales=“{format-number(sum(region/@sales), ‘$####0.00’)}”/>
For this to work correctly under XSLT 1.0, you need to add
data-type=“number”
to the
Output
I have added line breaks for readability:
See Also
xsl:strip-space
The
Changes in 2.0
The syntax of a
NameTest
has been extended to allow the format
*:NCName
, which matches all elements with a given local name, in any namespace.
Format
elements = tokens />
Position
Attributes
Name | Value | Meaning |
elements mandatory | Whitespace-separated list of NameTests | Defines elements in the source document whose whitespace-only text nodes are to be removed |
The construct
NameTest
is defined in XPath, and is described in Chapter 9, on page 614.
Content
None, the element is always empty.
Effect, Usage, and Examples
See
See Also
xsl:stylesheet
The
Changes in 2.0
A number of new attributes have been added:
xpath-default-namespace
,
default-collation
,
default-validation
,
input-type-annotation
, and
use-when
. Several of these attributes, although usually used on the