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

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

Within the predicate, just as within an

instruction in XSLT, the context item is the item from the sequence that's being tested; the context position is the position of that item in the sequence being filtered (counting from one); and the context size is the number of items in the sequence. This means, for example, that you can select the first half of the sequence by writing:

$sequence[position() * 2 <= last()]

There is a special rule for predicates, namely that if the value of the predicate is a number
N
, then it is treated as a shorthand for the condition
[position()=
N
]
, which selects the
N
th item in the sequence.

For path expressions of the form
A/B
, the rules are the same as the rules for predicates. The expres sion B is evaluated once for each node in the sequence produced by evaluating A, and while B is being evaluated, that node is the context item, the position of that node in the sequence is the context position, and the number of items in the sequence is the context size. However, it's very hard to construct a useful path expression that actually uses
position()
or
last()
on the right-hand side of the
/
operator. Using them inside a predicate such as
A/B[last()]
doesn't count, of course, because the focus changes again once you're inside the predicate.

It's also important to be aware that certain expressions
don't
change the focus. Specifically, the focus is not changed within a
for
,
some
, or
every
expression (these expressions are described in Chapter 10). So the expression:

Other books

Sinner by Ted Dekker
Quarter Horse by Bonnie Bryant
Bitter Blood by Rachel Caine
I Want to Kill the Dog by Cohen, Richard M.
La reina de la Oscuridad by Margaret Weis & Tracy Hickman
The Eye in the Door by Pat Barker
Forever Yours (#3) by Longford , Deila
Under the Empyrean Sky by Chuck Wendig
The Marquess by Patricia Rice