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.
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://example.com/</loc></url>
</urlset>//locThe 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.
//sm:locBind 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.
//*[local-name()='loc' and namespace-uri()='http://www.sitemaps.org/schemas/sitemap/0.9']Prove the fix
- Run the expressionCompare the empty unprefixed result with the namespace-aware form.
- Map every pathSee the namespace URI beside each extracted node path.
- Read the XPath referenceContinue through functions, axes, and operators executed on libxml2.
Related guides
- XPath first match vs first match per parentWhy //item[1] can return several nodes while (//item)[1] returns one, and how predicate context changes the result.
- How to add hreflang to an XML sitemapBuild complete alternate-language clusters with xhtml:link, self-references, reciprocal links, and absolute canonical URLs.
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.