You'll remember from the lesson on location paths that each location path is made up of location steps. Location steps can also include an axis.
In this lesson, we'll look at XPath axes (plural for axis).
What is an Axis?
An axis stores certain information about the context node or other nodes within the document. The information it stores depends on the axis being used. For example, an axis called "child" stores information about the children of the context node. Therefore, we could use this axis to select a child from the context node.
You use an axis by using it's name, along with a node test, and optionally, one or more predicates. The axis and node test are separated by
::, and the predicate/s are enclosed in
Axis without predicates - Here's the syntax you use if you don't have any predicates:
Axis with predicates - Here's the syntax you use if you have one or more predicates:
Up until now, our location steps have specified the exact nodes in the path, or they have used a wildcard to specify unknown nodes. For example, they have been constructed something like this:
We have other options when building our XPath expressions - we could use an axis.
Consider the following expression:
This expression looks a little different to the ones we've looked at so far. The expression is selecting a node called "title", but only when it's a child of the context node (remember that context node refers to the current node).
You might be thinking, "Yeah that's fine, but how do I know what the context node is?"
Here's an example of how we could use the axis mentioned above:
You'll remember from our XSLT lessons that this statement is selecting the value from a node within the "book" context. We know we're in the "book" context because of the
match="book" (which is selecting a node called "book"). Therefore, we can explicitly state that we want to select the "title" node which is a child of the context node.
List of Axes
There are many other axes you can use within your XPath expressions. Here's a list of the axes you can use with XPath:
|ancestor||Contains the ancestors of the context node. Ancestors include the parent, and it's parent, and it's parent etc all the way back up to the root node.|
|ancestor-or-self||Contains the context node and it's ancestors.|
|attribute||Contains the attributes of the context node.|
|child||Contains the children of the context node.|
|descendant||Contains the descendants of the context node. Descendants include the node's children, and that child's children, and it's children etc (until there are no more children)|
|descendant-or-self||Contains the context node and it's descendants.|
|following||Contains all nodes that come after the context node (i.e. after it's closing tag).|
|following-sibling||Contains the following siblings of the context node. Siblings are at the same level as the context node and share it's parent.|
|namespace||Contains the namespace of the context node.|
|parent||Contains the parent of the context node.|
|preceding||Contains all nodes that come before the context node (i.e. before it's opening tag).|
|self||Contains the context node.|