XPath Axes

Axes in XPath (which we will describe shortly) provide an indication of the direction we, or an XPath processor, are to travel around the tree representation of an XML document.

Directions of Axes

Axes can be viewed as being forward—that is, in document order—or reverse—that is, in the opposite direction of document order.

Proximity Position

The direction of an axis that forms part of a location step influences how any position in a predicate in that same location step is interpreted.

In a forward axis, a predicate [2] means that the second node in document order is selected.

<SEUsingXML> 
<Chapter>The XML Jigsaw Puzzle</Chapter>
<Chapter>The Basics of XML</Chapter>
<Chapter>XML Building Blocks</Chapter>
<Chapter>Structuring XML Documents with DTDs</Chapter>
<Chapter>XML Schemas</Chapter>
<Chapter>XML Namespaces</Chapter>
</SEUsingXML>

For example, if the context node were the Chapter element node with the text content XML Building Blocks, the code

following::Chapter[2] 

would select the Chapter element node that has a text node containing XML Schemas. The following axis is a forward axis, so we are moving toward the end of the document. As we go forward into the document, we meet a Chapter element node with text content Structuring XML Documents with DTDs, and the second node has the text content XML Schemas.

If the axis in the location step is a reverse axis, the predicate [2] would mean that the second node in reverse document order is selected by the predicate. If we again start at the third Chapter element node, the code

preceding::Chapter[2] 

is as if we turned around to face the beginning of the document. The first Chapter element we meet has the text context of The Basics of XML. We carry on going toward the beginning of the document, and the second Chapter element node has the text content of The XML Jigsaw Puzzle.

Predicates

The direction of an axis also affects the way in which XPath predicates are interpreted as just described for proximity position.

Axes in XPath

In XPath 1.0, there are thirteen axes. These will now be described individually.

The child Axis

The child axis contains the children of the context node. The child axis represents an element nested immediately within another element. The child axis is a forward axis.

<Book> 
 <Chapter>
  <Paragraph>
   Some text.
  </Paragraph>
 </Chapter>
</Book>

If the Book element node is the context node, the Chapter element node is in the child axis. The Paragraph element node is not in the child axis because it isn't a direct child of the Book element node.

Nodes that can occur in the child axis are element nodes, processing instruction nodes, comment nodes, and text nodes. Attribute nodes and namespace nodes cannot occur in the child axis.

The descendant Axis

The descendant axis includes all nodes that are descendants of the context node. A descendant is a child node of the context node, that child node's child nodes, and so on. The descendant axis is a forward axis.

The parent Axis

The parent axis selects the parent node of the context node. The parent axis is a reverse axis.

If, for example, the context node was an element node corresponding to the paragraph element in the following code, the parent node is the element node corresponding to the chapter element.

<chapter> 
 <paragraph>
  Some paragraph text about XML
 </paragraph>
</chapter>

Only two types of nodes are found in the parent axis of any node—a root node or an element node—because only those types of nodes can be parent nodes.

The ancestor Axis

The ancestor axis includes all the ancestor nodes of the context node. An ancestor node is the parent of the context node, that node's parent node, and so on. The ancestor axis always stops at the root node because the root node has no parent node and therefore no ancestor nodes. The ancestor axis is a reverse axis.

The following Axis

The following axis selects nodes that follow the context node in document order. Descendants of the context node are not included in the results. It also excludes attribute nodes or namespace nodes. The following axis is a forward axis.

If the context node is an attribute node or a namespace node, the following axis is empty.

The code that follows will allow us to explore the use of the following axis.

<Book> 
<Chapter number="1">
<Paragraph number="1">
Some text.
</Paragraph>
<Paragraph number="2">
Some extra text.
</Paragraph>
</Chapter>
<Chapter number="2">
<Paragraph number="1">
Some additional text.
</Paragraph>
<Paragraph number="2">
Some more text.
</Paragraph>
</Chapter>
</Book>

If the first Paragraph element node was the context node and the path expression was following::Paragraph, three nodes would be returned in the resultant node-set. The second Paragraph element node associated with Chapter 1 and both Paragraph element nodes associated with Chapter 2 would be included in the node-set because they are not in the descendant axis of the context node and occur later than the context node in document order.

The following-sibling Axis

The following-sibling axis selects nodes that are not present in the descendant axis of the context node and also share a parent with the context node. It also excludes attribute nodes and namespace nodes. The following-sibling axis is a forward axis.

If the context node is an attribute node or a namespace node, the following-sibling axis is empty.

Thus if we applied the path expression following-sibling::Paragraph to the document shown in the preceding section, the resulting node-set would only contain a single Paragraph element node—the second Paragraph element node associated with Chapter 1. The two Paragraph element nodes associated with Chapter 2 would not be included because those nodes do not share a common parent node with the context node.

The preceding Axis

The preceding axis selects nodes that are not in the ancestor axis of the context node and appear before it in document order. It also excludes attribute nodes or namespace nodes. The preceding axis is a reverse axis.

If the context node is an attribute node or a namespace node, the preceding axis is empty.

Let's use the example in the “following Axis” section again. If the second Paragraph element node relating to Chapter 2 was the context node and the path expression was preceding::Paragraph, three nodes would be returned in the resultant node-set. The first Paragraph element node associated with Chapter 2 and both Paragraph element nodes associated with Chapter 1 would be included in the node-set because they are not present in the ancestor axis for the context node and occur earlier than the context node in document order.

The preceding-sibling Axis

The preceding-sibling axis selects nodes that are not present in either the ancestor axis of the context node, which occur earlier in document order and also share a parent with the context node. It also excludes attribute nodes or namespace nodes. The preceding-sibling axis is a reverse axis.

If the context node is an attribute node or a namespace node, the preceding-sibling axis is empty.

Thus if we applied the path expression preceding-sibling::Paragraph to the same document we have been working with in the previous examples, the resulting node-set would only contain a single Paragraph element node—the first Paragraph element node associated with Chapter 2. The two Paragraph element nodes associated with Chapter 1 would not be included because those nodes do not share a common parent node with the context node.

The attribute Axis

The attribute axis selects attribute nodes associated with the context node. If the context node is not an element node, the attribute axis is empty.

The namespace Axis

The namespace axis selects namespace nodes associated with the context node. If the context node is not an element node, the namespace axis is empty.

The self Axis

The self axis refers to only the context node itself.

The descendant-or-self Axis

The descendant-or-self axis is the union of the descendant axis and the self axis. The descendant-or-self axis is a forward axis.

The ancestor-or-self Axis

The ancestor-or-self axis is the union of the ancestor axis and the self axis. The ancestor-or-self axis is a reverse axis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247