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

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 XsltLanguage and XmlPointerLanguage? (XPointer). It can also be used as a query language with an XmlDatabase.

The full spec and more examples can be found at http://www.w3.org/TR/xpath.

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, XqueryLanguage 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: http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Is it a LogicProgrammingLanguage?

No. It's more of a LittleLanguage than anything else, far closer to a UniformResourceIdentifier, for instance, than it is to PrologLanguage. 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 http://www.winnetmag.com/Articles/Print.cfm?ArticleID=16023

Beginner QuickQuestions

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

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

And can I use XsltLanguage without using XpathLanguage?

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 RubyLanguage: http://wiki.rubygarden.org/Ruby/page/show/XpathExplorer

FireFox has an awesome visual XPath explorer, "XPath Checker", at https://addons.mozilla.org/en-US/firefox/addon/1095 . Here's a blog entry on how to use it with AssertXpath?:



View edit of October 9, 2009 or FindPage with title or text search