Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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

    0
  • Matching 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.

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.