Home arrow XML arrow Page 4 - Navigating Input Documents Using Paths

Navigating Input Documents Using Paths

If you want to learn how to extract information from XML documents, you'll want to read this three-part series. It covers path expressions. This article is excerpted from chapter four of the book XQuery, written by Priscilla Walmsley (O'Reilly, 2007; ISBN: 0596006349). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
February 21, 2008
  1. · Navigating Input Documents Using Paths
  2. · Steps and changing context
  3. · Node Tests
  4. · Abbreviated Syntax

print this article

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

Unabbreviated syntaxAbbreviated equivalent

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:

  doc("catalog.xml")/catalog/product/(number | name)

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:

  product/substring(name,1,30)/replace(.,' ','-')

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.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials