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

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

There are two very similar constructs in XPath that use predicates. They can be used in an
AxisStep
, to qualify the nodes selected by the axis, and they can be used in a
FilterExpr
, to filter any sequence. We will talk about the more general filter expressions in Chapter 10 and concentrate here on the use of predicates with an
AxisStep
. The meaning of the two cases is very similar, and it's easy to use them without always being aware of the difference.

For example:

Expression
Description
para[position() > 1]
Here the predicate
[position() > 1]
is being applied to the
AxisStep
para
, which is short for
./child::para
. It selects all the

element children of the context node except the first. Because the expression is an
AxisStep
, the results are guaranteed to be in document order and to contain no duplicates.
$para[position() > 1]
Here the predicate
[position() > 1]
is being applied to the value of the variable-reference
$para
. The expression selects all items in the sequence except the first. The result does not have to be in document order (it can contain atomic values as well as nodes, so document order would not make sense), and it can contain duplicates. The items in the result are returned in their original order.

In both cases the effect of a predicate is to select a subset of the items in a sequence. There's a significant difference when a predicate is used with a path expression of more than one step. For example:

Expression
Description
chapter/para[1]
Here the predicate
[1]
is being applied to the
Step
para
, which is short for
./child::para
. It selects the first child

element of each child

element of the context node.
(chapter/para)[1]
This is a
FilterStep
where the predicate
[1]
is being applied to the sequence of nodes selected by the path expression
chapter/para
. The expression selects a single

element, the first child

of a

that is a child of the context node.

Other books

Westward Dreams by Linda Bridey
Bible Stories for Adults by James Morrow
Matchplay by Madison, Dakota
The Greek Myths, Volume 1 by Robert Graves
The Beta by Annie Nicholas
Tinker by Wen Spencer
Dagon by Fred Chappell