Introduction to Filtering with XPath Predicates
From MSXML 5.0 SDK
Introduction to Filtering with XPath Predicates
An XPath location path consists of one or more location steps, delimited by slashes. For example:
/child::book/child::chapter/attribute::date_written
is an XPath location path which returns a node-set consisting of all date_written attributes of those <chapter> elements which themselves are children of the root <book> element.
Note This example could also be expressed using XPath's abbreviated syntax as:
/book/chapter/@date_written
Remaining examples in this topic will use the abbreviated syntax. For more information, see Using Abbreviations in XPath Expressions.
In general, the purpose of a location path is to return a set of one or more nodes—a node-set—meeting specified criteria. Each location step in the location path consists of an axis (explicit or defaulted to child::) relative to the context node, a node test, and an optional predicate. (The above example does not include a predicate in any of the three location steps.) All nodes which meet all criteria specified by the full location path are returned by the expression.
The general format of a predicate in a location step is:
[condition]
where the square brackets, [ and ], are required, and condition represents some test typically resulting in a Boolean true or false value. The condition usually takes the form:
value operator value
If a particular condition itself may be represented as a Boolean true or false value, the predicate can take a simpler form:
[value]
Note This simplified form of the predicate is very important in limiting a location path only to nodes which have other nodes along a particular axis. If there are any nodes of the given name or type along the indicated axis, the result is true; if there are no such nodes, the result is false. Thus:
chapter[descendant::figure]
selects<chapter>elements which are children of the context node only if they have any descendant<figure>elements.
As an example, the set of nodes returned by the location path at the beginning of this topic could be further limited as follows:
/book/chapter[position()>1]/@date_written[.="2001-05-12"]
This location path can be read, from left to right:
- Locate the root
<book>element (/book). - Locate all
<chapter>elements but the first, in document order, which are children of the root<book>element located by Step 1 (chapter[position()>1]).Note A predicate, like everything else in an XML document, must adhere to the XML rules for well-formed documents. Therefore, operators such as > (in this example), <, and &, which have special meaning to XML parsers, must be escaped using entity references such as
>,<, and&respectively. - For the
<chapter>elements selected by Step 2, locate all of theirdate_writtenattributes whose values equal the string"2001-05-12"(@date_written[.="2001-05-12"]).
The result of evaluating this location path is a node-set consisting of the date_written attributes (if any) meeting all these criteria. If no date_written attributes meet the criteria, the result is an empty node-set.
White space between the value and operator terms in a predicate is not significant. Thus, the following two predicates produce the same result:
[position() = 1] [position()=1]
See Also
Use XPath Axes to Navigate through XML Data | Determining the Context Node | Constructing the Node-Test Portion of a Location Step