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

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.

Use a bound prefix for a known vocabulary

The prefix in the XPath belongs to the evaluation context and does not need to match the source prefix. Binding s to the sitemap URI makes //s:loc precise, readable, and resistant to unrelated loc elements from extensions.

Precise namespaced selection
//s:loc

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.
Namespace-agnostic fallback
//*[local-name() = 'loc']
URI-constrained fallback
//*[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.

Primary sources

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.