Share via


Axes

A location path uses an axis to specify the relationship between the nodes selected by the location step and the context node.

Axes Description

ancestor::

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; thus the ancestor:: axis always includes the root node unless the context node is the root node.

ancestor-or-self::

The context node and its ancestors.

The ancestor-or-self:: axis always includes the root node.

attribute::

The attributes of the context node.

This axis will be empty unless the context node is an element.

child::

The children of the context node.

A child is any node immediately below the context node in the tree. However, neither attribute or namespace nodes are considered children of the context node.

descendant::

The descendants of the context node.

A descendant is a child or a child of a child and so on; thus the descendant:: axis never contains attribute or namespace nodes.

descendant-or-self::

The context node and its descendants.

following::

All nodes that are after the context node in the tree, excluding any descendants, attribute nodes, and namespace nodes.

following-sibling::

All the following siblings of the context node.

The following-sibling:: axis identifies just those children of a parent node who appear in the tree after the context node. This axis excludes all other children that appear before the context node.

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

namespace::

The namespace nodes of the context node.

There is a namespace node for every namespace which is in scope for the context node.

This axis will be empty unless the context node is an element.

parent::

The parent of the context node, if there is one.

The parent is the node immediately above the context node in the tree.

preceding::

All nodes that are before the context node in the tree, excluding any ancestors, attribute nodes, and namespace nodes.

One way to think of the preceding axis is all nodes whose content occurs in their entirety before the start of the context node.

preceding-sibling::

All the preceding siblings of the context node.

The preceding-sibling:: axis identifies just those children of a parent node who appear in the tree before the context node. This axis excludes all other children that appear after the context node.

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

self::

Just the context node itself.

Remarks

  • namespace
    Each element has an associated set of namespace nodes, one for each distinct namespace prefix that is in scope for the element (including the xml prefix, which is implicitly declared by the XML Namespaces Recommendation) and one for the default namespace if one is in scope for the element. The element is the parent of each of these namespace nodes; however, a namespace node is not a child of its parent element. Elements never share namespace nodes: if one element node is not the same node as another element node, then none of the namespace nodes of the one element node will be the same node as the namespace nodes of another element node. This means that an element will have a namespace node:

    • For every attribute on the element whose name starts with xmlns:.

    • For every attribute on an ancestor element whose name starts with xmlns: — unless the element itself or a nearer ancestor redeclares the prefix.

    • For an xmlns attribute, if the element or some ancestor has an xmlns attribute, and the value of the xmlns attribute for the nearest such element is non-empty.

The <sample> element in the following instance:

<A xmlns="urn:A">
  <B xmlns:test="sample">
    <sample xmlns:sampletest="sampletest"/>
  </B>
</A>

contains the following namespace nodes:

xmlns="urn:A"
xmlns:test="sample"
xmlns:sampletest="sampletest"

The <sample> element in the following instance:

<A xmlns="urn:A">
  <B  xmlns:test="sample">
    <sample xmlns:sampletest="sampletest" xmlns:bar="test"/>
  </B>
</A>

contains the following namespace nodes:

xmlns:sampletest="sampletest"

xmlns:test="test"

Note

An attribute undeclares the default namespace.

A namespace node has an expanded-name: the local part is the namespace prefix (this is empty if the namespace node is for the default namespace); the namespace Uniform Resource Identifier (URI) is always null.

The string-value of a namespace node is the namespace URI that is being bound to the namespace prefix; if it is relative, it must be resolved just like a namespace URI in an expanded-name.

Examples

following::

The examples for the following:: axis refer to this instance document:

<A>
  <B>
    <C>sample</C>
    <C>sample2</C>
  </B>
  <B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
  </B>
</A>
Query Returned nodes

A/B[1]/following::*

<B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
</B>
<C>sample</C>
<C>sample2</C>

A/B[1]/following::node()

<B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
</B>
<C>sample</C>
sample
<C>sample2</C>
sample2
<D>sample3</D>

preceding::

The example for the preceding:: axis refer to this instance document:

<A>
  <B>
    <C test="sampletest">sample</C>
    <C>sample2</C>
  </B>
  <B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
  </B>
</A>
Query Returned nodes

A/B[2]/preceding::*

<B>
  <C test="sampletest">sample</C>
  <C>sample2</C>
</B>
<C>sample</C>

following-sibling::

The example for the following-sibling:: axis refers to this instance document:

<A>
  <B>
    <C test="sampletest">sample</C>
    <C>sample2</C>
  </B>
  <B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
  </B>
</A>
Query Returned nodes

A/B[1]/following-sibling::*

<B>
  <C>sample</C>
  <C>sample2</C>
  <D>sample3</D>
</B>

preceding-sibling::

The example for the preceding-sibling:: axis refers to this instance document:

<A>
  <B>
    <C test="sampletest">sample</C>
    <C>sample2</C>
  </B>
  <B>
    <C>sample</C>
    <C>sample2</C>
    <D>sample3</D>
  </B>
</A>
Query Returned nodes

A/B[2]/preceding-sibling::*

<B>
  <C test="sampletest">sample</C>
  <C>sample2</C>
</B>

See Also

Reference

Location Steps
Node Tests
Predicates