Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Constructing a Multi-step XPath Expression

Location paths can contain more than one location step, separated from one another by slashes. Each succeeding location step, reading the pattern from left to right, is evaluated in the context of the nodes selected by all the location steps which precede it.

Consider the following location path:

person[@birthdate <= "1951-06-18"]/surname[substring(., 1, 1)="S"]

This location path consists of two location steps. We have already seen in this section the first step (preceding the slash). It locates a node-set consisting of only one node, or none, or several. If there is at least one node in the resulting node-set, the second step (following the slash) kicks in, and restricts the list of matching nodes even further. Specifically, of the node(s) selected by the first step, the location path as a whole selects only those (if any) with a child named <surname> whose content begins with the letter "S".

Note that the above location path moves you down the document tree, from the context node, to its <person> child(ren), to the <surname> child(ren) of each of the matching <person> elements. Because of the wide variety of XPath axes, however, this downward direction is not the only option available to you. For example, a location path might be constructed as follows:

person[@birthdate &lt;= "1951-06-18"]/surname[substring(., 1, 1)="S"]/../preceding-sibling::*

The two addition location steps in this path move you from the node(s) selected in the earlier example up to their parents (..), and then sideways to include only the nodes which are preceding siblings of those parents (preceding-sibling::*).

See Also

Use XPath Axes to Navigate through XML Data | Constructing the Node-Test Portion of a Location Step | Filtering XML Data Using XPath Predicates