One of the fundamental constructs for XSL transformations and XML links, XPath is nonetheless one of the lesser lights of the XML universe. However, if you're serious about developing your XML skills, you need to know it inside out - and this tutorial has all you need to get started.
Since the first component of a location step is the axis, let's deal with that first. An axis defines the relationship between the current node and the nodes to be selected - whether, for example, they are children of the current node, siblings of the current node, or the parent of the current node.
The XPath specification defines the following axes:
self - the "self" axis refers to the context node itself;
parent - the "parent" axis selects the parent of the context node;
child - the "child" axis selects the children of the context node;
attribute - the "attribute" axis refers to the attributes of the context node;
These are the most commonly-used ones; however, it's quite likely that you'll also find a use for:
ancestor - the "ancestor" axis selects the parent, grandparent, great-grandparent and all other ancestors of the context node;
descendant - the opposite of the "ancestor" axis, this axis selects the children (and children's children) of the context node;
ancestor-or-self - this variant of the "ancestor" axis selects all ancestors of the context node as well as the node itself;
descendant-or-self - this variant of the "descendant" axis selects all descendants of the context node as well as the node itself;
following-sibling and preceding-sibling - these two axes contain the nodes at the same level in the document tree as the context node. Depending on which axis you use, you will get a collection of siblings which are either after or before the context node, in document order.
following and preceding - the "following" axis selects all nodes within the document tree which follow (are placed after) the context node, while the "preceding" axis selects all nodes which come before the context node.
namespace - the "namespace" axis selects all the nodes in the same namespace as the context node;
Once the relationship to be established has been defined and an appropriate node collection obtained, a node test can be used to further filter the items in the collection. This node test is connected to the axis by a double colon (::) symbol.
Typically, you would select a node on the basis of its name without using a node test; in such a case, the node type is deduced from the axis part of the location step. If, on the other hand, you'd like to select nodes on the basis of type, XPath offers some pre-defined node tests; the text() node test selects text nodes, the comment() function selects comments, the processing-instruction() function selects PIs and the generic node() function selects any and all nodes.
Finally, in case the resulting collection needs to be further broken down, XPath allows you to add optional predicates to each location step, enclosed within square braces.
This article copyright Melonfire 2001. All rights reserved.