XML instance document
We will use this XML document in the following example:
<?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
The axis defines a node set relative to the current node.
Axis name |
Result |
Ancestor |
Select all the ancestors of the current node (parent, grandfather, etc) |
Ancestor-or-self |
Select all the founders of the current node (parent, grandfather, etc.) and 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 (child, sun, etc.) of the current node ). |
Descendant-or-self |
Select all descendant elements (child, sun, etc.) of the current node and the current node itself. |
Following |
Select all nodes after the end label 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 before the start label of the current node in the document. |
Preceding-sibling |
Select all peer nodes before 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 (/), but the relative path does not. In either case, the path contains one or more steps, and each step is separated by a slash:
Absolute path:
/step/step/...
Relative Path:
step/step/...
Each step is calculated based on the nodes in the current node set.
Steps include:
-
Axis)
-
Define the tree relationship between the selected node and the current node
-
Node-test)
-
Recognizes nodes inside a certain axis
-
Zero or more predicates (predicate)
-
Further refine the selected node set
Step Syntax:
Axis name: node test [predicates]
Instance
Example |
Result |
Child: Book |
Select the book nodes of all 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 subnodes of the current node |
Child: node () |
Select All subnodes of the current node |
Descendant: Book |
Select All book descendants of the current node |
Ancestor: Book |
Select All book predecessors of the current node |
Ancestor-or-self: Book |
Select all the book predecessors of the current node and the current node (if this node is a book node) |
Child: */child: Price |
Select all price grandchildren of the current node. |