XPath guide
XPath local-name() vs namespace prefixes
Choose explicit namespace bindings for precise XPath, and reserve local-name() fallbacks for deliberate namespace-agnostic inspection.
7 min read · Updated 2026-08-21
The short answer
Prefer a namespace prefix bound to the exact URI because it selects the intended vocabulary. Use local-name() only when namespace identity is genuinely unknown or intentionally irrelevant, and pair it with namespace-uri() whenever same-named elements from different vocabularies could appear.Know what local-name() discards
//*[local-name()='loc'] matches every element whose local part is loc, regardless of namespace. That can be useful while exploring unknown XML, but in mixed vocabularies it may combine semantically unrelated nodes into one result.
- Use local-name() for discovery, migrations, or intentionally version-tolerant tooling.
- Add namespace-uri() when the vocabulary still matters.
- Do not scatter long URI predicates through production queries when a prefix map is available.
//*[local-name() = 'loc']//*[local-name() = 'loc' and namespace-uri() = 'http://www.sitemaps.org/schemas/sitemap/0.9']Centralize the namespace map
Keep the application's namespace bindings beside its XPath expressions and reuse them across evaluation, tests, and transformations. When a vocabulary version changes URI, that map becomes the single explicit migration point.
Test with a second namespace that reuses the same local name. A query that should be vocabulary-specific must not return that decoy node.
Prove the fix
- Run both expressionsCompare results after adding a same-local-name element in another namespace.
- Read local-name()See the function's actual result on the shared XPath sample.
- Read namespace-uri()Use the namespace half of the expanded name when filtering.
Related guides
- Why XPath matches nothing in a default namespaceThe XPath 1.0 namespace rule behind an empty //loc result, with the prefix-bound solution and the local-name fallback.
- XML namespace prefix vs URIUnderstand why prefixes are replaceable aliases, why namespace URIs are compared literally, and how expanded names keep documents interoperable.
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.