Skip to main content

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

XPath guide

Why XPath matches nothing in a default namespace

The XPath 1.0 namespace rule behind an empty //loc result, with the prefix-bound solution and the local-name fallback.

7 min read · Updated 2026-08-21

The short answer

In XPath 1.0, an unprefixed element name means an element in no namespace. A default namespace in the XML does not become the default namespace for the XPath expression. Bind that URI to a prefix in the evaluator and use the prefix, or match local-name() and namespace-uri() when the host cannot bind namespaces.

The document and expression use different rules

The loc elements below are in the sitemap namespace because urlset declares a default namespace. The expression //loc asks for loc elements in no namespace, so an empty result is correct rather than a parser bug.

Namespaced document
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc>https://example.com/</loc></url>
</urlset>
Matches nothing
//loc

The expression asks for loc in no namespace.

Bind a prefix when the host allows it

The prefix used by the XPath does not need to appear in the document. Bind sm to the sitemap URI in your library or tool, then query //sm:loc. The URI, not the spelling sm, is what connects the names.

Namespace-aware expression
//sm:loc

Bind sm to http://www.sitemaps.org/schemas/sitemap/0.9 in the evaluator.

Use the fallback deliberately

Some hosts give an XPath string but no namespace-binding API. In that case match both the local name and namespace URI. Matching only local-name() is shorter but also accepts a same-named element from an unrelated vocabulary.

Precise fallback
//*[local-name()='loc' and namespace-uri()='http://www.sitemaps.org/schemas/sitemap/0.9']

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.