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.

Location paths and steps

Figure 3-2. Location paths and steps


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: ...

Get XForms Essentials now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.