Xpath Language

XML Path Language, or XPath, is a language for addressing parts of an XML document. It's sort of an "XML query language".

An XPath expression specifies a set of "nodes". For example, the expression

/child::chapter[child::title='Introduction']

selects all chapter elements of a document that have one or more title subelements with string-value equal to "Introduction".

Relational operators and functions are provided that support fairly sophisticated queries. For example, the expression

/descendant::Invoice[sum(child::Item) > 20]

selects all Invoice elements where the sum of the values of their Item subelements is greater than 20.

XPath was designed to support Xslt Language and Xml Pointer Language (XPointer). It can also be used as a query language with an Xml Database.

The full spec and more examples can be found at www.w3.org .


The primary purpose of XPath is to address parts of an XML document. In support of this primary purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.

But what does it mean? Is it from a class of languages?

It's used in XSLT, Xquery Language and some other XML languages, but it's used lots of places. It's a simple language that allows users and programmers to find parts of XML documents without manual traversal. There's a great tutorial with many examples: www.zvon.org

No. It's more of a Little Language than anything else, far closer to a Uniform Resource Identifier, for instance, than it is to Prolog Language. It's a way to refer to parts of XML documents, in the same way that a URI is a way to refer to specific location on the Internet, though that comparison doesn't really do XPath justice. XPaths can include wildcards and perform substring matching and comparisons. Maybe it's SQL for trees?


Reading Material

Retrieving Nodes... (Beginner level material) at www.winnetmag.com


I have seen in an article that Xpath Language v2 is overly complicated and should be avoided, yet the same person said Xpath Language v1 (as found in Xslt Language) is used everywhere.

Will a software that claim support for Xpath Language v2 have to include full support for Xpath Language v1?

And can I use Xslt Language without using Xpath Language?

No, but it wouldn't make any sense not to. XSLT needs a way to specify nodes and grab and manipulate data, so it uses (a superset of) Xpath.


Every implementation of the spec has an "Explorer" that lets one edit a path into an XML sample and interactively display the results. Here's one for Ruby Language: wiki.rubygarden.org

Fire Fox has an awesome visual XPath explorer, "XPath Checker", at addons.mozilla.org . Here's a blog entry on how to use it with Assert Xpath:


See original on c2.com