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

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
TABLE OF CONTENTS:
  1. · Navigating Input Documents Using Paths
  2. · Steps and changing context
  3. · Node Tests
  4. · Abbreviated Syntax

print this article
SEARCH DEVARTICLES

Navigating Input Documents Using Paths - Steps and changing context
(Page 2 of 4 )

The context item changes with each step. A step returns a sequence of zero, one, or more nodes that serve as the context items for evaluating the next step. For example, in:

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

the doc("catalog.xml") step returns one document node that serves as the context item when evaluating the catalog step. The catalog step is evaluated using the document node as the current context node, returning a sequence of one catalog element child of the document node. This catalog element then serves as the context node for evaluation of the product step, which returns the sequence of product children of catalog.

The final step, number, is evaluated in turn for each product child in this sequence. During this process, the processor keeps track of three things:

  • The context node itself--for example, the product element that is currently being processed
  • The context sequence, which is the sequence of items currently being processed--for example, all the product elements
  • The position of the context node within the context sequence, which can be used to retrieve nodes based on their position
Steps

As we have seen in previous examples, steps in a path can simply be primary expressions like function calls (doc("catalog.xml")) or variable references ($catalog). Any expression that returns nodes can be on the lefthand side of the slash operator.

Another kind of step is the axis step, which allows you to navigate around the XML node hierarchy. There are two kinds of axis steps:

Forward step
  
This step selects descendents or nodes appearing
   after the context node (or the context node itself).

Reverse step
  
This step selects ancestors or nodes appearing
   before the context node (or the context node itself).

In the examples so far, catalog, product, and @dept are all axis steps (that happen to be forward steps). The syntax of an axis step is shown in Figure 4-1.


Figure 4-1.  Syntax of a step in a path expression

Axes

Each forward or reverse step has an axis, which defines the direction and relationship of the selected nodes. For example, the child:: axis (a forward axis) can be used to indicate that only child nodes should be selected, while the parent:: axis (a reverse axis) can be used to indicate that only the parent node should be selected. The 12 axes are listed in Table 4-2.

Table 4-2. Axes

Axis

Meaning

self::

The context node itself.

child::

Children of the context node. Attributes are not considered children of an element. This is the default axis if none is specified.

descendant::

All descendants of the context node (children, children of children, etc.). Attributes are not considered descendants.

descendant-or-self::

The context node and its descendants.

attribute::

Attributes of the context node (if any).

 

Table 4-2. Axes (continued)

AxisMeaning
following::All nodes that follow the context node in the document, minus the context nodes descendants.
following-sibling::All siblings of the context node that follow it. Attributes of the same element are not considered siblings.
parent::The parent of the context node (if any). This is either the element or the document node that contains it. The parent of an attribute is its element, even though it is not considered a child of that element.
ancestor::All ancestors of the context node (parent, parent of the parent, etc.).
ancestor-or-self::The context node and all its ancestors.
preceding::All nodes that precede the context node in the document, minus the context nodes ancestors.
preceding-sibling::All the siblings of the context node that precede it. Attributes of the same element are not considered siblings.

An additional forward axis, namespace, is supported (but deprecated) by XPath 2.0 but not supported at all by XQuery 1.0. It allows you to access the in-scope namespaces of a node.

Implementations are not required to support the following axes: following, following-sibling, ancestor, ancestor-or-self, preceding, and preceding-sibling.


blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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