An XPath expression consists of a series of one or more location steps, separated by "/" delimiters; together, these steps are referred to as the location path. So a location path consisting of two location steps looks like:
locationstep/locationstep
Each location step, from left to right, relocates you up or down in the document's tree of nodes relative to the location step(s) preceding it, if any.
A location step can be absolute or relative. Absolute location steps can never appear in the location path in any but the leftmost position. Relative location steps can appear anywhere in the location path.
Location paths can be compoundthat is, you can construct a location path which will select any nodes matching any of a group of constituent location paths. To do so, separate the location paths with a pipe character, |. For example:
locationpath1 | locationpath2
says to locate all nodes in the target document on either locationpath1 or locationpath2.
Some sample absolute location paths and their interpretations are provided in the following table.
Location path | Description |
---|---|
/ |
Locates the document root |
/books |
Locates all <books> children of the document root |
/descendant::title |
Locates all <title> elements in the document (i.e., descended from the document root) |
/comment() | /text() |
Locates all comments and text nodes in the document |
/processing-instruction("xml-stylesheet") |
Locates any processing instruction whose target is "xml-stylesheet" . |
An absolute location step in an XPath expression assumes that the content will be located "starting from scratch" within the target document. It always begins with a "/" delimiter, whose presence is interpreted to mean, "Start at the document's root node."
The starting point for a relative location step is always considered to be some previously established context node. This context node may be identified by an absolute location step, or, if the context node within the target document has already been set, by some relative location step. For more information, see Context and Navigation in XPath.