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

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

A numeric predicate
[P]
is simply a shorthand for the boolean predicate
[position() = P]
, so you could also achieve the required effect by writing
[position() = @sequence-number]
.

Note that the rules for recognizing a predicate as a number are very strict. For example, a string written as
“20”
is not considered to be a number: it's the type label on the value that matters, not the format of the value itself. Equally, the XSLT variable declared in the example below is not a number, it is the document node at the root of a temporary tree (see the

topic in Chapter 6):

3

If you want to use this value as a predicate, either write it so the value of the variable is a number:


(but don't write
select = “‘3’”
, because that would make it a string) or force it to a number in the predicate. Any of the following will work:




As explained in Chapter 7, every expression is evaluated with a particular focus. The focus for evaluating the predicate is not the same as the focus for the expression that it forms part of. The predicate is applied separately to each node selected by the axis, and each time it is evaluated:

  • The context item (the item selected by
    .
    ) is the node to which the predicate is being applied.
  • The context position (the result of the
    position()
    function) is the number assigned to that node within the sequence of nodes.
  • The context size (the result of the
    last()
    function) is the number of nodes in the sequence.

As we saw earlier the number assigned to a node selected by an
AxisStep
depends on the direction of the axis used in that
AxisStep
. Some axes (child, descendant, descendant-or-self, following, following-sibling) are forward axes, so the
position()
function numbers the nodes in document order. Other axes (ancestor, ancestor-or-self, preceding, preceding-sibling) are reverse axes, so
position()
numbers them in reverse document order. The self and parent axes return a single node, so the order is irrelevant. The ordering of nodes on the attribute and namespace axes is undefined, so positional predicates on these axes don't make much sense, though they are permitted.

Other books

Survivor Planet III by Juliet Cardin
Devil in the Details by Jennifer Traig
The Abyssinian Proof by Jenny White
The Fire Within by Wentworth, Patricia
Let Me In by Carolyn Faulkner
Smithy's Cupboard by Ray Clift
Sisterchicks Do the Hula by Robin Jones Gunn
The Book Club Murders by Leslie Nagel