Constructing the Predicate Portion of a Location Step
The final possible component of an XPath location step, called the predicate, provides a further, finer-grained filter to select among the node(s) located by the node-test and axis. The basic form of a predicate is:
[something operator somevalue]
where:
- the square brackets, [ and ], are required;
- something may be either a further location step, relative to the nodes matched so far by the node-test and axis, or a built-in XPath function;
- operator is one of several Boolean operators, such as an equals sign or "!=" for a "does not equal" condition; and
- somevalue is the value to which you want to compare something.
For instance:
[position() = 1]
locates the first matching node in document order, while:
[attribute::empid != "73519"]
locates the subset of the matching node-set at that point which includes any nodes with an empid
attribute whose string value does not equal "73519".
Note Be careful when using the "<" and ">" operators in your XPath expressions. Because these characters are significant in markup, you will typically have to escape them using the<
and>
entity references. For example:
[attribute::empid <= "69999"]
says to select all nodes with an empid
attribute whose value is <= (less than or equal to) "69999".
White space between the terms of a predicate is not significant. That is:
[position() = 1]
and
[position()=1]
function identically.
See Also
Filtering XML Data Using XPath Predicates