Location Paths
A key requirement for dissecting
nearly any XPath expression is an understanding of Location
Paths, which select one or more nodes based on their
location or other properties. A Location Path consists of a number of
individual Location Steps, each separated by a
slash (/). Each
individual step builds upon the previous steps to traverse the
document, and can be a test against the name of a node, or one of the
following special tests:
-
node( ) Matches any node whatsoever.
-
text( ) Matches any text node.
-
comment( ) Matches any comment node.
-
processing-instruction( ) Matches any processing instruction node, and may have a parameter to match against a specific processing instruction.
Another special test is
*, which will match any
element node (or attribute node within the
attribute axis, or namespace node within the
namespace axis.) Similarly, another special test
prefix:* will
match any node identified with the namespace mapped to
prefix.
Figure 3-2 illustrates how a path is traversed in steps, from left to right.

Figure 3-2. Location paths and steps
Warning
Extra care is needed when traversing a document that contains XML namespaces, especially with defaulted namespaces. Any namespace prefixes in scope can be used in Location Steps; however, default namespaces in scope do not apply to the XPath expression.
For example, to address the item elements in this example: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access