XPath function
name()
The node's qualified name, prefix included — which makes it dependent on how the document happens to be written.
name() returns the QName as it appears in the document: 'sm:loc' if that prefix was used, 'loc' if the element was unprefixed. That makes it convenient for inspection and unreliable for matching, because the prefix is the document author's choice and carries no meaning of its own.
Two documents can be semantically identical and disagree about every prefix. Matching on name() couples your expression to one of them.
- Syntax
name(node-set?) → string- Returns
- string
- Kind
- Function
- Version
- XPath 1.0 (libxml2)
What catches people out
- Prefixes are arbitrary. Matching name()='sm:loc' breaks the moment a document uses a different prefix for the same namespace — use local-name() and namespace-uri().
- For a default-namespaced element, name() returns the bare local name, so it looks unprefixed even though it is in a namespace.
The sample documents
Every example on this page runs against these documents. 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"?>
<order id="A-1001" tier="gold">
<customer country="NO">Ada Lovelace</customer>
<lines>
<line sku="WIDGET-1" qty="2"><price>14.50</price></line>
<line sku="WIDGET-2" qty="1"><price>39.00</price></line>
<line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
</lines>
<note> Deliver after 09:00 </note>
</order><?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
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.