XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (240 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.46Mb size Format: txt, pdf, ePub


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:

  • One is to generate or modify the stylesheet before compiling it, so that it includes the actual sort key required. This technique is popular when transformations are executed in the browser, typically under the control of JavaScript code in the HTML page. The stylesheet is then typically loaded from the server and parsed into a DOM document, which can be modified
    in situ
    before the transformation starts. The disadvantage is that this means recompiling the stylesheet each time it is run: this would probably be an unacceptable overhead if the transformation is running server-side within a Web server.
  • Another is to use an extension function that permits the evaluation of XPath expressions that have been constructed dynamically, as strings. Such a function,
    dyn:evaluate()
    , is defined in the third-party function library at
    http://www.exslt.org/
    , and is available in this or a similar form with a number of XSLT processors including Saxon and Xalan.

Examples

I'll start with a couple of simple examples and then show a full working example that you can download and try yourself.

  • Example 1: Process all the

    children of the current node, sorting them by the value of the
    isbn
    attribute:


   


  • Example 2: Output the contents of all the

    elements in the document, in alphabetical order, including each distinct city once only:



       

       




If
select=“.”
were omitted from the

element, the effect would be the same, because this is the default, but I prefer to include it for clarity.

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

element sorts the selected nodes (all the

elements) in descending order of the numerical total of the
sales
attribute over all their

child elements. The total is calculated using the
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

element. This is not needed with XSLT 2.0 because the type is recognized automatically.

Output

I have added line breaks for readability:


  

  

  


See Also


on page 240


on page 322


on page 326


on page 437

xsl:strip-space

The

declaration, along with

, is used to control the way in which whitespace nodes in the source document are handled. The

declaration identifies elements in which whitespace-only text nodes are considered insignificant, so they can be removed from the source tree.

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


is a top-level declaration, which means it is always a child of the

element. There are no constraints on where it appears relative to other declarations.

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

on page 439. The two elements

and

are closely related, so I have presented the rules and usage guidance in one place.

See Also


on page 439


on page 492

xsl:stylesheet

The

element is the outermost element of a stylesheet. The synonym

can be used as an alternative.

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

element and described here for convenience, can actually be used on any XSLT element in the stylesheet.

Other books

Rush by Maya Banks
Mine to Tarnish by Falor, Janeal
Dirty by HJ Bellus
The Wedding Hoax by Heather Thurmeier
(2005) Wrapped in Rain by Charles Martin
Crossing by Andrew Xia Fukuda
Rampant by Gemma James
A Most Inconvenient Marriage by Regina Jennings