Parsing and processing
XPath
The expression language for addressing parts of an XML document — the query language inside XSLT, XSD's identity constraints, Schematron and every DOM API.
An XPath expression names a path through the tree and returns a node-set, a string, a number or a boolean. Version 1.0 is the one embedded almost everywhere, including in libxml2 and therefore in this site's tools; 2.0 and 3.1 are far richer and far less universally available.
The single biggest trap is namespaces. In XPath 1.0 an unprefixed name means "in no namespace", so a path copied out of a namespaced document — every sitemap, every Atom feed — matches nothing at all, with no error to explain it.
- Defined in
- XPath 1.0
- Category
- Parsing and processing
- Also written
- XML Path Language
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
In a namespaced document, the obvious path returns an empty node-set rather than an error.
document.xml <feed xmlns="http://www.w3.org/2005/Atom"><entry><title>Hello</title></entry></feed>expression count(//title)What the engine returns
0Matching on the local name gets you there.
document.xml <feed xmlns="http://www.w3.org/2005/Atom"><entry><title>Hello</title></entry></feed>expression string(//*[local-name()='title'])What the engine returns
Hello
What catches people out
- XPath 1.0 has no lower-case(), matches() or ends-with(). Those are 2.0, and calling one gets you an unregistered-function error.
- An empty node-set is a result, not a failure. Most engines report it as though nothing went wrong, because nothing did.
- Positional predicates count along the axis, so [1] on a reverse axis is the nearest node rather than the first in the document.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- XPathlocal-name()The function that gets you past the namespace trap.
- XPathPredicates [ ]The other half of most real expressions.
- ToolsXPath testerRun an expression against your own document, on libxml2.
Related terms
- NamespaceA URI that qualifies element and attribute names, so identically-named elements from different vocabularies can coexist in one document.
- DOMAn API that parses a whole document into a navigable tree in memory, where every node can be reached from any other.
- XSLTA language for transforming XML into other XML, HTML or text, written as templates that match patterns in the source tree.
- SchematronA rule-based schema language that asserts XPath conditions about a document, rather than describing its grammar.
- Document orderThe order nodes appear in the serialized document, which is the order node-sets are returned in and the order positional predicates count in.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.