XPath Axes (Axes)
- Previous Page XPath Syntax
- Next Page XPath Operators
XML instance document
We will use this XML document in the following examples:
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
XPath axis
An axis can define a set of nodes relative to the current node.
Axis name | Result |
---|---|
ancestor | Select all ancestors (parent, grandfather, etc.) of the current node. |
ancestor-or-self | Select all ancestors (parent, grandfather, etc.) of the current node, as well as the current node itself. |
attribute | Select all attributes of the current node. |
child | Select all child elements of the current node. |
descendant | Select all descendant elements (children, grandchildren, etc.) of the current node. |
descendant-or-self | Select all descendant elements (children, grandchildren, etc.) of the current node, as well as the current node itself. |
following | Select all nodes following the end tag of the current node in the document. |
namespace | Select all namespace nodes of the current node. |
parent | Select the parent node of the current node. |
preceding | Select all nodes preceding the start tag of the current node in the document. |
preceding-sibling | Select all sibling nodes preceding the current node. |
self | Select the current node. |
Location path expression
The location path can be absolute or relative.
The absolute path starts with a forward slash (/), while the relative path does not. In both cases, the location path includes one or more steps, each separated by a slash:
Absolute Position Path:
/step/step/...
Relative Position Path:
step/step/...
Each step is calculated based on the nodes in the current node set.
Step (step) includes:
- Axis (axis)
- Define the tree relationship between the selected node and the current node
- Node Test (node-test)
- Identify nodes within a certain axis
- Zero or more predicates (predicate)
- Further refine the selected node set
Step Syntax:
Axis Name::Node Test[Predicate]
Instance
Example | Result |
---|---|
child::book | Select all 'book' elements that are children of the current node. |
attribute::lang | Select the 'lang' attribute of the current node. |
child::* | Select all child elements of the current node. |
attribute::* | Select all attributes of the current node. |
child::text() | Select all text child nodes of the current node. |
child::node() | Select all child nodes of the current node. |
descendant::book | Select all descendants of the 'book' element. |
ancestor::book | Select all ancestors of the 'book' element. |
ancestor-or-self::book | Select all ancestors of the 'book' element and the current node (if this node is a 'book' node). |
child::*/child::price | Select all 'price' child nodes of the current node. |
- Previous Page XPath Syntax
- Next Page XPath Operators