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

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

The direction of the axis used to select the nodes is irrelevant. (The direction of different axes is described in Chapter 9.) For example,
select=“preceding-sibling::*”
will process the preceding siblings of the context node in document order (starting with the first sibling) even though the preceding-sibling axis is in reverse document order. The axis direction affects only the meaning of any positional qualifiers used within the select expression. So
select=“preceding-sibling::*[1]”
will select the first preceding sibling element in the direction of the axis, which is the element immediately before the context node, if there is one.

Although most XPath expressions return nodes in document order, not all do so. For example, the expression
title, author, publisher
returns a sequence containing first the child
title
elements, then the child
author
elements, and then the child
publisher
elements of the context node, regardless of the order that these nodes appear in the source document. The items returned by the XPath expression will be processed in the order of the sequence that is returned, not necessarily in document order.

If there are one or more

instructions as children of the

instruction, the items are sorted before processing. Each

instruction defines one component of the sort key. If the sort key contains several components, they apply in major-to-minor order. For example, if the first

defines sorting by country and the second by state, then the nodes will be processed in order of state within country. If two items have equal sort keys (or if the same item is included more than once in the sequence), they will be processed in the order that they appeared in the original result of the
select
expression, unless the first

element specifies
stable=“no”
, in which case there are no guarantees. For a more complete specification of how sorting works, see

on page 455.

If you want to process the items in the reverse of their original order, specify:


Alternatively, call the
reverse()
function. The following instruction will process the preceding siblings of the context node in reverse document order (that is, starting with the sibling closest to the context node and working backward).


Usage and Examples

The main purpose of

is to iterate over a sequence of items. It can also be used, however, simply to change the context item. These two styles of use are illustrated in the following sections.

Iterating over a Sequence of Nodes

The most common use of

is to iterate over a sequence of nodes. As such it provides an alternative to

. Which you use is largely a matter of personal style; arguably

(
push
processing) ties the stylesheet less strongly to the detailed structure of the source document and makes it easier to write a stylesheet that can accommodate some flexibility in the structures that will be encountered, while

(
pull
processing) makes the logic clearer to the reader. It may even improve performance because it bypasses the need to identify template rules by pattern matching, though the effect is likely to be very small.

The following example processes all the attributes of the current element node, writing them out as elements to the result tree. This example is presented in greater detail under

on page 315.


   

      

         

                      namespace=“{namespace-uri()}”>

            

         

      

   


The next example is a general one that can be applied to any XML document.

Example: Showing the Ancestors of a Node

The following example stylesheet can be applied to any XML document. For each element it processes all its ancestor elements, in reverse document order (that is, starting with the parent node and ending with the document element), and outputs their names in a comment that shows the position of the current node.

Source

This stylesheet can be applied to any source document.

Stylesheet

This stylesheet is in the file
nesting.xsl
.

 version=“2.0”

>


   

      

      

         

          within 

         

      

   

   



Output

An example of the output this might produce is:


   

   Number, the

                                           Language of Science

   Danzig

   5.95

   3

Other books

Testament by Nino Ricci
The Summer Wind by Mary Alice Monroe
This Side of Jordan by Monte Schulz
The Yellow House Mystery by Gertrude Warner
Bittersweet Endeavors by Tamara Ternie
Fatally Flaky by Diane Mott Davidson
Bloodcraft by Amalie Howard
The Wedding Runaway by Katy Madison