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

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

To understand the meaning of positional predicates in the step (for example
[3]
) it is often useful to think of an axis as retrieving nodes in a particular order, but the formal definition doesn't require this. Instead these predicates are defined in terms of a number assigned to each node. For a forward axis (as shown in the syntax above), the nodes are numbered to show their relative position in document order, while for a reverse axis, they are numbered in reverse document order. The effect of positional predicates (such as
booklist/book[3]
) is to select those nodes whose number matches the value of the predicate. This means that if the axis is a forward axis, the positional predicate
[3]
will return the node that is third in document order; if it is a reverse axis, the same predicate will return the node that is third in reverse document order.

So the evaluation of the axis step, for a given context node, proceeds as follows:

1.
All the nodes on the selected axis are found, starting at the context node.

2.
Those that satisfy the node test (that is, those of the required node kind, name, and type) are selected.

3.
The remaining nodes are numbered from 1 to N in document order if the axis is a forward axis, or in reverse document order if it is a reverse axis.

4.
The first (leftmost) predicate is applied to each node in turn. When evaluating the predicate, the context node (that is, the result of the
.
expression) is that node, the context position (the result of the
position()
function) is the number assigned to the node in stage 3, and the context size (the result of the
last()
function) is the largest number allocated in stage 3. A numeric predicate such as
[2]
or
[last()-1]
is interpreted as a shorthand for
[position() = 2]
or
[position() = last()-1]
, respectively. The node is selected if the predicate is
true
, and it is discarded if the predicate is
false
.

5.
Stages 3 and 4 are repeated for any further predicates. For each predicate, the nodes that survive to this stage are renumbered 1 to N, in document order for a forwards axis, or reverse document order for a reverse axis.

Other books

The Redeeming by Tamara Leigh
A Flight of Arrows by Lori Benton
Keeper of the Light by Diane Chamberlain
The Biology of Luck by Jacob M. Appel
Casting Shadows by Sophie McKenzie