SunQuest
 
       XML
  Home arrow XML arrow Page 2 - Navigating Input Documents Using Paths
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Navigating Input Documents Using Paths
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-21

    Table of Contents:
  • Navigating Input Documents Using Paths
  • Steps and changing context
  • Node Tests
  • Abbreviated Syntax

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

    More XML Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "XQuery," published by O'Reilly. We hope...
     

    XML ARTICLES

    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands
    - Creating an XUL App Installer
    - Overlays in XUL
    - Skinning Your Custom XUL Applications







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway