Axis defines a node-set relative to the current node.
Xpath Axes
There are thirteen different axes in the XPath specification. An axis
represents a relationship to the context node, and is used to locate nodes
relative to the tree. The following is an extremely brief
description of thirteen available axes
A D V E R T I S E M E N T
Ancestor
This indicates all the ancestors of the context node beginning with the parent node
and traveling through to the root node.
Ancestor-or-self
Indicates the context node and all of its ancestors, including the root node.
Attribute
Indicates the attributes of the context node.
This axis can be abbreviated with the at sign (@).
Child
This Indicates the children of the context node. If an XPath expression does not
specify an axis,then it is understood by default. Since only the root node or element
nodes have children,any other use will select nothing.
Descendant
Indicates all the children of the context node, and all of their children,
and so forth. Attribute and namespace nodes are not included - the parent of an
attribute node is an element node, but attribute nodes are not the children of
their parents.
Descendant-or-self
Indicates the context node and all of its descendants. Attribute and namespace
nodes are not included - the parent of an attribute node is an element node, but
attribute nodes are not the children of their parents.
Following
Indicates all the nodes that appear after the context node, except any
descendant, attribute, and namespace nodes.
Following-sibling
Indicates all the nodes that have the same parent as the context node and appear
after the context node in the source document.
Namespace
Indicates all the nodes that are in scope for the context node. In this case,
the context node must be an element node.
Parent
Indicates the single node that is the parent of the context node. It can be
abbreviated as two periods (..).
preceding
Indicates all the nodes that precede the context node in the document except any
ancestor, attribute and namespace nodes.
preceding-sibling
Indicates all the nodes that have the same parent as the context node and appear
before the context node in the source document.
self
Indicates the context node itself. It can be abbreviated as a single period (.).
Location Path Expression
A location path can be relative or absolute.
An absolute location path starts with a slash ( / ) but relative location path
does not. In both cases the location path consists of one or more steps, each
separated by a slash:
An absolute location path:
/step/step/...
A relative location path:
step/step/...
All step is evaluated against the nodes in the current node-set.
A step consists of:
An axis which defines the tree-relationship between the selected nodes and
the current node
node-test which identifies a node within an axis
Zero or more predicates for further refine the selected node-set
Syntax of Location step:
axisname::nodetest[predicate]
Example:
Example
Result
child::book
Selects all book nodes that are children of the current
node
attribute::lang
Selects the lang attribute of the current node
child::*
Selects all children of the current node
attribute::*
Selects all attributes of the current node
child::text()
Selects all text child nodes of the current node
child::node()
Selects all child nodes of the current node
descendant::book
Selects all book descendants of the current node
ancestor::book
Selects all book ancestors of the current node
ancestor-or-self::book
Selects all book ancestors of the current node - and
the current as well if it is a book node
child::*/child::price
Selects all price grandchildren of the current node