Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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
select
expression, unless the first
stable=“no”
, in which case there are no guarantees. For a more complete specification of how sorting works, see
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
Iterating over a Sequence of Nodes
The most common use of
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
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”
>
Output
An example of the output this might produce is:
Number, the
Language of Science
Danzig
5.95
3