XPath

Go back to Tutorial

XPath (XML Path Language) is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C). XPath contains over 200 built-in functions.

The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as “an XPath”.

The most important kind of expression in XPath is a location path. A location path consists of a sequence of location steps. Each location step has three components:

  • an axis
  • a node test
  • zero or more predicates.

An XPath expression is evaluated with respect to a context node. An Axis Specifier such as ‘child’ or ‘descendant’ specifies the direction to navigate from the context node. The node test and the predicate are used to filter the nodes specified by the axis specifier: For example, the node test ‘A’ requires that all nodes navigated to must have label ‘A’. A predicate can be used to specify that the selected nodes have certain properties, which are specified by XPath expressions themselves.

The XPath syntax comes in two flavours: the abbreviated syntax, is more compact and allows XPaths to be written and read easily using intuitive and, in many cases, familiar characters and constructs. The full syntax is more verbose, but allows for more options to be specified, and is more descriptive if read carefully.

Abbreviated Syntax

The compact notation allows many defaults and abbreviations for common cases. Given source XML containing at least

<A>

<B>

<C/>

</B>

</A>

the simplest XPath takes a form such as

/A/B/C

that selects C elements that are children of B elements that are children of the A element that forms the outermost element of the XML document. The XPath syntax is designed to mimic URI (Uniform Resource Identifier) and Unix-style file path syntax.

More complex expressions can be constructed by specifying an axis other than the default ‘child’ axis, a node test other than a simple name, or predicates, which can be written in square brackets after any step. For example, the expression

A//B/*[1]

selects the first child (‘*[1]’), whatever its name, of every B element that itself is a child or other, deeper descendant (‘//’) of an A element that is a child of the current context node (the expression does not begin with a ‘/’). Note that the predicate [1] binds more tightly than the / operator. To select the first node selected by the expression A//B/*, write (A//B/*)[1]. Note also, index values in XPath predicates (technically, ‘proximity positions’ of XPath node sets) start from 1, not 0 as common in languages like C and Java.

Expanded Syntax

In the full, unabbreviated syntax, the two examples above would be written

/child::A/child::B/child::C

child::A/descendant-or-self::node()/child::B/child::*[position()=1]

Axis Specifiers

Axis specifiers indicate navigation direction within the tree representation of the XML document.  The axes available are

 

Full Syntax Abbreviated Syntax Notes
ancestor Indicates all the ancestors of the context node beginning with the parent node and traveling through to the root node.
ancestor-or-self Indicates the context node and all of its ancestors, including the root node.
attribute @ @abc is short for attribute::abc Indicates the attributes of the context node. Only elements have attributes.
child xyz is short for child::xyz Indicates the children of the context node. If an XPath expression does not specify an axis, this is understood by default.
descendant Indicates all of the children of the context node, and all of their children, and so forth. Attribute and namespace nodes are not included – the parent of an attribute node is an element node, but attribute nodes are not the children of their parents.
descendant-or-self // // is short for /descendant-or-self::node()/Indicates the context node and all of its descendants. Attribute and namespace nodes are not included – the parent of an attribute node is an element node, but attribute nodes are not the children of their parents.
following Indicates all the nodes that appear after the context node, except any descendant, attribute, and namespace nodes.
following-sibling Indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.
namespace Indicates all the nodes that are in scope for the context node.
parent .. .. is short for parent::node()  Indicates the single node that is the parent of the context node.
preceding Indicates all the nodes that precede the context node in the document except any ancestor, attribute and namespace nodes.
preceding-sibling Indicates all the nodes that have the same parent as the context node and appear before the context node in the source document.
self . . is short for self::node()   Indicates the context node itself.

Location Path

Location path specifies the location of node in XML document. This path can be absolute or relative. If location path starts with root node or with ‘/’ then it is an absolute path.

An absolute xpath in HTML DOM starts with /html e.g.

/html/body/div[5]/div[2]/div/div[2]/div[2]/h2[1]

and a relative xpath finds the closed id to the DOM element and generates xpath starting from that element e.g.

.//*[@id=’answers’]/h2[1]/a[1]

Create XPATH locators to identify page elements

Go back to Tutorial

Get industry recognized certification – Contact us

Menu