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><title lang="eng">Harry Potter</title> <price>29.99</price> </book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
XPath axis
An axis can define a node set relative to the current node. | Result |
---|---|
Axis name | 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. | Select all attributes of the current node. |
attribute | Select all child elements of the current node. |
child | 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.
Position path expression
The position path can be absolute or relative.
Absolute position path: The absolute path starts with a forward slash (/), while the relative path does not. In both cases, the position path includes one or more steps, each separated by a slash:
/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 nodes that belong to the child elements 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 current node that are 'book' nodes. |
ancestor::book | Select all ancestors of the current node that are 'book' nodes. |
ancestor-or-self::book | Select all ancestors of the current node as well as 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