XML and Databases

Ronald Bourret

Consulting, writing, and research in XML and databases

XML Guild

Member

XPath in Five Paragraphs

Copyright November, 2001 by Ronald Bourret

An XPath expresssion is a series of location steps separated by slashes (/). Each step selects a set of nodes in relation to the current node. These nodes become the current node(s) for the next step. The set of nodes selected by the expression are the nodes remaining after processing each step in order.

For example, the expression child::Part consists of a single step. This step selects the set of all Part elements that are children of the current node. The expression parent::node()/child::Part has two steps. The first step (parent::node()) selects a single node -- the parent of the current node. The second step (child::Part) selects all Part elements that are children of that node. Thus, the expression identifies all Part children of the parent of the current node. In other words, it selects all Part siblings of the current node, possibly including the current node.

A step in an XPath expression consists of three parts: an axis, a node test, and zero or more predicates. The axis specifies the direction to move in the document tree (which is roughly the same as the DOM document tree). For example, the child axis says to look at all child nodes. The parent axis says to look at the parent node. The self axis says to look at the current node. The descendant axis says to look at all descendant nodes. And so on.

A node test tests whether nodes encountered along the specified axis should be selected for the next step. For example, in child::Part, child is the axis and Part is the node test. A child node satisfies the node test if it is an element with the name of Part. There are node tests that check the element, attribute, and namespace name, as well as tests that check if the node is a text, comment, or processing instruction node.

A predicate is an expression that filters nodes selected by the node test. It takes the form of an equality (=, !=, >, <, >=, <=) expression. For example, [self::text()="123"] tests if the text of the current node is "123". And [child::PartNumber/self::text()="123"] tests if the current node has a PartNumber child with text of "123". Thus, the XPath expression child::PartNumber[self::text()="123"] selects all PartNumber children of the current node with text "123". And (more informally) child::Part[child::PartNumber/self::text()="123"] selects all parts that are children of the current node and have a part number of "123".

For more information, see the XPath recommendation.


Copyright (c) 2009, Ronald Bourret