XPath function
local-name()
An element's name without its namespace prefix — and the standard way to match namespaced documents when you cannot bind a prefix.
local-name() returns the local part of a node's expanded name: for <sm:loc> it is 'loc', and for an unprefixed <loc> it is also 'loc'. With no argument it applies to the context node.
This is the workaround that makes XPath usable against namespaced documents from a command line or a config file, where there is nowhere to declare a prefix. //*[local-name()='loc'] matches the element regardless of which namespace it is in — and regardless of whether the document uses a prefix at all.
It is a workaround rather than the correct answer: it will also match a <loc> from an entirely different namespace. Where you can bind a prefix, bind one.
- Syntax
local-name(node-set?) → string- Returns
- string
- Kind
- Function
- Version
- XPath 1.0 (libxml2)
Worked examples
Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.
What catches people out
- It ignores the namespace entirely, so it will match a same-named element from a different vocabulary. Combine with namespace-uri() when that matters.
- local-name() of an attribute node returns the attribute's local name, which is usually not what you want when filtering elements.
- Applied to an empty node-set it returns the empty string rather than raising an error, so a typo produces silence.
The sample documents
Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://example.com/</loc><lastmod>2026-01-01</lastmod></url>
<url><loc>https://example.com/pricing</loc></url>
</urlset>Related
- name()The node's qualified name, prefix included — which makes it dependent on how the document happens to be written.
- namespace-uri()The namespace URI a node is in — the half of its identity that actually carries meaning.
- count()How many nodes a node-set contains. The fastest way to check whether an expression matches what you think it does.
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.