Filter expressions
The notation E[P]
is used to select items from the sequence obtained by evaluating
E
. If the predicate P
is numeric, the predicate selects an item if its
position (counting from 1) is equal to P
; otherwise, the effective boolean value
of P
determines whether an item is selected or not. The effective boolean value of a sequence
is false if the sequence is empty, or if it contains a single item that is one of: the boolean value
false, the zero-length string, or a numeric zero or NaN value. If the first item of the sequence is a node, or
if the sequence is a singleton boolean, number or string other than those listed above, the effective
boolean value is true. In other cases (for example, if the sequence contains two booleans or a date),
evaluating the effective boolean value causes an error.
From XPath 2.0, E
may be any sequence, it is not restricted to a node sequence. Within
the predicate, the expression .
(dot) refers to the context item, that is, the item
currently being tested. The XPath 1.0 concept of context node has thus been generalized, for example
.
can refer to a string or a number.
Generally the order of items in the result preserves the order of items in E
. As a
special case, however, if E
is a step using a reverse axis (e.g. preceding-sibling), the
position of nodes for the purpose of evaluating the predicate is in reverse document order, but the
result of the filter expression is in forwards document order.
In XPath 3.0, the postfix operators []
(filter) and ()
(function call)
may be mixed and apply in left-to-right order. For example $a[3](2)
selects the third
item in a sequence (of function items), and calls it with the argument value 2; while $a(2)[3]
calls the function item $a
with argument value 2, and then selects the third item in the resulting
sequence.