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
13.36Mb 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

Strictly Business by Adrienne Maitresse
Emako Blue by Brenda Woods
The Serpent's Bite by Warren Adler
Bridge of Dreams by Bishop, Anne
Artemis Awakening by Lindskold, Jane
Anonyponymous by John Bemelmans Marciano
A Cowboy Comes Home by Barbara Dunlop
The Closer by Alan Mindell
Wednesdays in the Tower by Jessica Day George