XPath function
namespace-uri()
The namespace URI a node is in — the half of its identity that actually carries meaning.
A node's real name is the pair (namespace URI, local name). namespace-uri() returns the first half. Unlike a prefix, it is fixed by the specification the document conforms to, which makes it the right thing to match on.
Combined with local-name() it identifies an element exactly, and does so without needing a prefix to be bound anywhere.
- Syntax
namespace-uri(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.
namespace-uri(/*)on sitemap.xmlRunhttp://www.sitemaps.org/schemas/sitemap/0.9namespace-uri(/*)on order.xmlRun(empty node-set — no matches)
An empty node-set: the document declares no namespace, so the URI is the empty string and nothing is printed.
count(//*[local-name()='loc' and namespace-uri()='http://www.sitemaps.org/schemas/sitemap/0.9'])on sitemap.xmlRun2The precise form: local name and namespace together, with no prefix bound.
What catches people out
- For a node in no namespace this returns the empty string, which is falsy — so `namespace-uri()` alone is a usable test for 'is namespaced'.
- The URI is compared as a string, exactly. A trailing slash or a http/https difference makes it a different namespace, and the document is the authority on which one it used.
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"?>
<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><?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>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.