Select a node subset by using a predicate expression
You can use a Predicate
in a location step or in a filter
expression, both of which identify a node-set. This node-set is then restricted
by applying the PredicateExpr
can be used to filter a node set in
a location step.
A predicate can either be a Boolean expression or a numeric expression.
A numeric predicate [n]
is short for
[position()=n]
which is true for all
nodes that are the nth child element in the input
node set. If the value of the predicate is not a number, then it is treated as
a Boolean expression.
It corresponds to the expression Predicate
defined in
XPath, Section 2.4,
Rule 8.
Select all names in a patient's document (select all descendant child
name
nodes that are the first child element node of their
respective parent nodes; in our example database, this returns six
name
nodes):
//name[1]
Select the names of all patients (select all name
nodes
that are the first child element node of any child of the root element; this
restricts it effectively to /patient/name[1]
which returns two
name
nodes):
/*/name[1]
Select the names of all doctors (select all name
nodes
that are a child of any descendant doctor
element node of any
child of the root element and satisfy the following predicate expression: they
are the last child element of their parent doctor
element; since
there is only one name
node this is equivalent to
//doctor/name[position()=1]
so that three name
nodes
are returned):
//doctor/name[position()=last()]
Select all doctors with a pager (all descendant doctor
nodes that have a pager
attribute node attached):
//name[@pager]
FilterExpr |
Step |