Navigating Input Documents Using Paths - Abbreviated Syntax (Page 4 of 4 )
Some axes and steps can be abbreviated, as shown in Table 4-3. The abbreviations "." and ".." are used as the entire step (with no node test). "." represents the current context node itself, regardless of its node kind. Likewise, the step ".." represents the parent node, which could be either an element node or a document node.
Table 4-3. Abbreviations
The @ abbreviation, on the other hand, replaces the axis only, so it is used along with a node test or wildcard. For example, you can use @dept to select dept attributes, or @* to select all attributes.
The // abbreviation is a shorthand to indicate a descendant anywhere in a tree. For example, catalog//number will match all number elements at any level among the descendants of catalog. You can start a path with .// if you want to limit the selection to descendants of the current context node.
Table 4-4 shows additional examples of abbreviated and unabbreviated syntax.
Table 4-4. Abbreviated and unabbreviated syntax examples
Other Expressions As Steps
In addition to axis steps, other expressions can also be used as steps. You have already seen this in use in:
where doc("catalog.xml") is a function call that is used as a step. You can include more complex expressions, for example:
which uses the parenthesized expression (number | name) to select all number and name elements. The | operator is a union operator; it selects the union of two sets of nodes.
If the expression in a step contains an operator with lower precedence than /, it needs to be in parentheses. Some other examples of more complex steps are provided in Table4-5.
Table 4-5. More complex steps (examples start with doc("catalog.xml")/catalog/)
product/(number | name)
All numberAND namechildren of product.
product/(* except number)
All children of productexcept number. See "Combining Results" in Chapter 9 for more information on the |and exceptoperators.
product/ (if (desc) then desc else name)
For each productelement, the descchild if it exists; otherwise, the namechild.
A sequence of xs:stringvalues that are substrings of product names.
The last step (and only the last step) in a path may return atomic values rather than nodes. The last example in Table 4-5 will return a sequence of atomic values that are the substrings of the product names. An error is raised if a step that is not the last returns atomic values. For example:
will raise an error because the substring step returns atomic values, and it is not the last step.
Please check back next week for the continuation of this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.