Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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 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 |
(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 |