- An expression language to be used in another host language (e.g., XSLT, XQuery).
- Allows the description of paths in an XML tree, and the retrieval of nodes that match these paths. Can also be used for performing some (limited) operations on XML data.
Example XPath Axes
2*3 is an XPath literal expression. //*[ @msg=”Hello world” ] is an XPath path expression, retrieving all elements with an msg attribute set to “Hello world”.
XPath Axes Data Model
XPath expressions operate over XML trees, which consist of the following node types:
- Document: the root node of the XML document;
- Element: element nodes;
- Attribute: attribute nodes, represented as children of an Element node;
- Text: text nodes, i.e., leaves of the XML tree.
An axis = a set of nodes determined from the context node, and an ordering of the sequence.
- child: (default axis).
- parent: Parent node.
- attribute: Attribute nodes.
- descendant: Descendants, excluding the node itself.
- descendant-or-self: Descendants, including the node itself.
- ancestor: Ancestors, excluding the node itself.
- ancestor-or-self: Ancestors, including the node itself.
- following: Following nodes in document order.
- following-sibling: Following siblings in document order.
- preceding: Preceding nodes in document order.
- preceding-sibling: Preceding siblings in document order.
- self: The context node itself.
- The child axis denotes the Element or Text children of the context node.
- Moreover, This is the default axis, used when the axis part of a step if not specified. So, child::D is, in fact, equivalent to D.
- The parent axis denotes the parent of the context node.
- Also, The result is always an Element or a Document node, or an empty node-set (if the parent does not match the node test or does not satisfy a predicate).
- One can use as node test an element name.
- Moreover, The node test * matches all names.
- The node test node() matches all node kinds.
These are the standard tests on element nodes. For instance:
- if the context node is one of the B elements,
- the result of parent:: A is the root element of our sample document; one obtains the same result with parent::* or parent:: node();
- Also, if the context node is the root element node,
- then parent::* returns an empty set, but the path parent:: node() returns the root node of the document.
- The attribute axis retrieves the attributes of the context node.
- The node test may be either the attribute name or @* which matches all attribute names.
- So, assuming the context node is the C element of our example, o @att1 returns the attribute named att1;
- @* returns the two attributes of the context node.
- The descendant axis denotes all nodes in the subtree of the context node, except the Attribute
- The node test text() matches any Text
- Assume for instance that the context node is the first B element in the document order.
- descendant:: node() retrieves all nodes descendants of the context node, except attributes;
- descendant::* retrieves all Element nodes, whatever their name, which is descendant of the context node;
- descendant:: text() retrieves all Text nodes, whatever their name, which is descendant of the context node.
- Observe that the context node is not a descendant of itself. If one wants it in the resulting context list, one should use instead descendant-or-self.
- The ancestor axis denotes all ancestor nodes of the context node.
- Also, The result of ancestor:: node(), when the context node is the first B element, consists of both the element root and the root node.
- Again, if one wants the context node to belong to the result, one should use ancestor-or-self
Following and preceding axes
- The following and preceding axes denote respectively all nodes that follow the context node in the document order, or that precede the context node, with the exception of descendant or ancestor nodes.
- Attribute nodes are not selected.
- The siblings of a node N are the nodes that have the same parent as N.
- Also, XPath proposes two axes: following-sibling and preceding-sibling that denote respectively the siblings that follow and precede the context node in document order.
- Moreover, The node test that can be associated with these axes is those already described for descendant or following: a node name (for Element), * for all names, text() or node(). Note that, as usual, the sibling axes do not apply to attributes.