Supported Axes

The XPath processor supports all XPath axes:

About the child Axis

The child axis contains the children of the context node. The following examples select the book children of the context node:

child::book
               
book
               

            

If the context node is the bookstore element, each of these queries return the book elements in bookstore.xml. When you do not specify an axis, the child axis is assumed.

About the descendant Axis

The descendant axis contains the descendants of the context node. A descendant is a child or a child of a child, and so on. The descendant axis never contains attribute nodes. The following example selects the first-name element descendants of the context node:

descendant::first-name
               

            

If the context node is the bookstore element, this query returns all first-name elements in the document. If the context node is the first publication element, this query returns the first-name element that is in the publication element.

About the parent Axis

The parent axis contains the parent of the context node, if there is one. The following example selects the parent of the context node if it is a title element:

parent::title
               

            

If the first title element in bookstore.xml is the context node, this query returns the first book element.

Note that dot dot ( ..) is equivalent to parent::node().

About the ancestor Axis

The ancestor axis contains the ancestors of the context node. The ancestors of the context node consist of the parent of the context node and the parent's parent, and so on. The ancestor axis always includes the root node, unless the context node is the root node. The following example selects the book ancestors of the context node:

ancestor::book
               

            

If the context node is the first title element in bookstore.xml, this query returns the first book element.

About the following-sibling Axis

The following-sibling axis contains all the siblings of the context node that come after the context node in document order. If the context node is an attribute node or namespace node, the following-sibling axis is empty. The following example selects the next book sibling of the context node:

following-sibling::book[position()=1] 
               

            

If the context node is the first book element in bookstore.xml, this query returns the second book element.

About the preceding-sibling Axis

The preceding-sibling axis contains all the siblings of the context node that precede the context node in reverse document order. If the context node is an attribute node or namespace node, the preceding-sibling axis is empty. The following example selects the closest previous book sibling of the context node:

preceding-sibling::book[position()=1] 
               

            

If the context node is the third book element in bookstore.xml, this query returns the second book element. If the context node is the first book element, this query returns the empty set.

About the following Axis

The following axis contains the nodes that follow the context node in document order. This can include

  • Following siblings of the context node
  • Descendants of following siblings of the context node
  • Following siblings of ancestor nodes
  • Descendants of following siblings of ancestor nodes

The following axis never includes

  • Ancestors or descendants of the context node
  • Attribute nodes
  • Namespace nodes

The following example selects the book elements that are following siblings of the context node and that follow the context node in document order:

following::book
               

            

If the context node is the first book element, this query returns the last three book elements. If the context node is the second book element, this query returns only the third and fourth book elements.

About the preceding Axis

The preceding axis contains the nodes that precede the context node in reverse document order. This can include:

  • Preceding siblings of the context node
  • Descendants of preceding siblings of the context node
  • Preceding siblings of ancestor nodes
  • Descendants of preceding siblings of ancestor nodes

The preceding axis never includes

  • Ancestors or descendants of the context node
  • Attribute nodes
  • Namespace nodes

The following example selects the book elements that are preceding siblings of the context node and that precede the context node in document order:

preceding::book
               

            

If the third book element is the context node, this query returns the first two book elements. If the first book element is the context node, this query returns the empty set.

About the attribute Axis

The attribute axis contains the attributes of the context node. The attribute axis is empty unless the context node is an element. The following examples are equivalent. They both select the style attributes of the context node. The at sign (@) is an abbreviation for the attribute axis.

attribute::style
               
@style
               

            

If the context node is the second book element, this query returns a style attribute whose value is textbook.

About the namespace Axis

The namespace axis contains the namespace nodes that are in scope for the context node. This includes namespace declaration attributes for the

  • Context node
  • Ancestors of the context node

If more than one declaration defines the same prefix, the resulting node set includes only the definition that is closest to the context node.

If the context node is not an element, the namespace axis is empty.

For example, if an element is in the scope of three namespace declarations, its namespace axis contains three namespace declaration attributes.

About the self Axis

The self axis contains just the context node itself. The following example selects the context node if it is a title element:

self::title
               

            

Note that dot ( .) is equivalent to self::node().

About the descendant-or-self Axis

The descendant-or-self axis contains the context node and the descendants of the context node. The following example selects the first-name element descendants of the context node and the context node itself if it is a first-name element:

descendant-or-self::first-name
               

            

If the context node is the first-name element that is in the author element in the second book element, this query returns just the context node. If the context node is the second book element, this query returns the two first-name elements contained in the second book element.

Note that // is equivalent to descendant-or-self::node(), while //name is equivalent to descendant-or-self::node()/child::name.

About the ancestor-or-self Axis

The ancestor-or-self axis contains the context node and the ancestors of the context node. The ancestor-or-self axis always includes the root node. The following example selects the author element ancestors of the context node and the context node itself if it is an author element:

ancestor-or-self::author
               

            

If the context node is the award element in the first book element, this query returns the first author element.

 
Free Stylus Studio XML Training: