This post is based on a tutorial at w3schools.com. An axis defines a node-set relative to the current node. For example, self selects the current node, and child selects all children of the current node.
The following XML document will be used in the following examples.
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book> <title lang="en">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="en">Learning XML</title> <price>39.95</price> </book> </bookstore>
|ancestor||Selects all ancestors (parent, grandparent, etc.) of the current node|
|ancestor-or-self||Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself|
|attribute||Selects all attributes of the current node|
|child||Selects all children of the current node|
|descendant||Selects all descendants (children, grandchildren, etc.) of the current node|
|descendant-or-self||Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself|
|following||Selects everything in the document after the closing tag of the current node|
|following-sibling||Selects all siblings after the current node|
|namespace||Selects all namespace nodes of the current node|
|parent||Selects the parent of the current node|
|preceding||Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes|
|preceding-sibling||Selects all siblings before the current node|
|self||Selects the current node|
Location Path Expression
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
An absolute location path: /step/step/... A relative location path: step/step/...
Each step is evaluated against the nodes in the current node-set. A step consists of:
- an axis (defines the tree-relationship between the selected nodes and the current node)
- a node-test (identifies a node within an axis)
- zero or more predicates (to further refine the selected node-set)
The syntax for a location step is:
An XPath expression returns either a node-set, a string, a Boolean, or a number.
||||Computes two node-sets||//book | //cd|
|+||Addition||6 + 4|
|–||Subtraction||6 – 4|
|*||Multiplication||6 * 4|
|div||Division||8 div 4|
|<=||Less than or equal to||price<=9.80|
|>=||Greater than or equal to||price>=9.80|
|or||or||price=9.80 or price=9.70|
|and||and||price>9.00 and price<9.90|
|mod||Modulus (division remainder)||5 mod 2|
For examples on how to use XPath in the browsers, please have a look at the tutrorial at w3school.com.