An axis represents a relationship to the context (current) node and it is used to locate nodes related to that node on the tree.
Axes are so named because they refer to the axis on which the elements related to an element are located.
List of various Axis Values
|Represents the ancestors of the current node which include the parents up to the root node.|
|Represents the current node and it's ancestors.|
|Represents the attributes of the current node.|
|Represents the children of the current node.|
|Represents the descendants of the current node. Descendants include the node's children upto the leaf node(no more children).|
|Represents the current node and it's descendants.|
|Represents all nodes that come after the current node.|
|Represents the following siblings of the context node. Siblings are at the same level as the current node and share it's parent.|
|Represents the namespace of the current node.|
|Represents the parent of the current node.|
|Represents all nodes that come before the current node (i.e. before it's opening tag).|
|Represents all siblings before the current node.|
|Represents the current node.|
Axis and Location Path Expression
As we explained in previous chapter XPath Paths, a location path can be absolute or relative.
An absolute location path starts with a slash ( / ) and a relative location path does not.
In both cases the location path consists of one or more steps, each separated by a slash:
- Absolute path:
- Relative path
Each step is evaluated against the nodes in the current node-set.
Each step must follow this syntax:
axisnameis an axis that defines the tree-relationship between the selected nodes and the current node.
nodetestidentifies a node within an axis.
predicatezero or more predicates to further refine the set of selected nodes.
You can learn more about predicates in XPath Predicate Chapter.
Consider the following XML document:
<?xml version="1.0" encoding="UTF-8"?>
<title lang="en">A Great Book</title>
<title lang="en">Learning XML</title>
In the following table there are some examples of selection using axes:
|Selects all book nodes that are children of the current node|
|Selects the lang attribute of the current node|
|Selects all element children of the current node|
|Selects all attributes of the current node|
|Selects all text node children of the current node|
|Selects all children of the current node|
|Selects all book descendants of the current node|
|Selects all book ancestors of the current node|
|Selects all book ancestors of the current node - and the current as well if it is a book node|
|Selects all price grandchildren of the current node|
Learn more about
node() and more in XPath Operators Chapter.