Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

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.

  • count(//*[local-name()='loc'])on sitemap.xmlRun
    2
  • //*[local-name()='lastmod']/text()on sitemap.xmlRun
    2026-01-01
  • local-name(/*)on sitemap.xmlRun
    urlset
  • count(//loc)on sitemap.xmlRun
    0

    For contrast: the unprefixed name matches nothing in a namespaced document.

Run your own expression

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.

sitemap.xml
<?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

All 34 XPath entries

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.