XPath

Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, subsets of the XPath query language are used in other W3C specifications such as XML Schema, XForms and the Internationalization Tag Set (ITS).

Each location step has three components: An XPath expression is evaluated with respect to a context node.

The XPath syntax comes in two flavors: 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.

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.

In the full, unabbreviated syntax, the two examples above would be written Here, in each step of the XPath, the axis (e.g. child or descendant-or-self) is explicitly specified, followed by :: and then the node test, such as A or node() in the examples above.

Here the same, but shorter: A//B/*[position()=1] Axis specifiers indicate navigation direction within the tree representation of the XML document.

(an abbreviation for self::node()) is most commonly used within a predicate to refer to the currently selected node.

For example, h3[.='See also'] selects an element called h3 in the current context, whose text content is See also.

Other node test formats are: Predicates, written as expressions in square brackets, can be used to filter a node-set according to some condition.

XPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and Booleans.

Given a sample XML document The XPath expression selects name attributes for all projects, and selects all editions of all projects, and selects addresses of all English Wikimedia projects (text of all edition elements where language attribute is equal to English).

XPath is increasingly used to express constraints in schema languages for XML.

XPath expression applied to an XML file