Xerobit
Data & Format

XPath Returns Nothing? Default XML Namespaces Explained

Why XPath finds nothing in an Atom feed: the default xmlns, with tested fixes in Python lxml, browser document.evaluate, C# XmlNamespaceManager and local-name(), plus XPath basics.

MA Mian Ali Khalid Updated Nov 2, 2026 7 min read
Free tool
XML Formatter
Format, validate, and beautify XML documents.
Open tool →
On this page ⌄
  1. Reproduce it
  2. Why it works this way
  3. Fix 1: map a prefix (recommended)
  4. Fix 2: local-name()
  5. Attributes and the URI
  6. XPath 1.0 essentials, tested on the same feed
  7. Check your document first
  8. Tested

You ran //title against an Atom feed and got zero nodes, though the element is plainly there. The cause is one attribute on the root, xmlns="http://www.w3.org/2005/Atom". XPath 1.0 does not apply a default namespace to names in a query, so title in your query means "a title in no namespace", and the elements in the document are in the Atom namespace. Everything below ran on 2026-11-02.

Reproduce it

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <entry><title>First</title><link href="http://example.org/1"/></entry>
  <entry><title>Second</title></entry>
</feed>

Python lxml 6.1.3 (libxml2 2.11.9):

//title                      -> 0 nodes
/feed/entry/title            -> 0 nodes
//a:title  (a = Atom URI)    -> 3 nodes: ['Example Feed', 'First', 'Second']
//*[local-name()="title"]    -> 3 nodes

Delete the xmlns attribute and //title finds 3. The query was fine; the document's names changed.

Why it works this way

Two rules from the W3C specs:

  • A default namespace declaration "applies to all unprefixed element names within its scope" (Namespaces in XML, section 6.2). So <title> in the feed is really {http://www.w3.org/2005/Atom}title.
  • In XPath 1.0 a name in a node test without a prefix gets a null namespace: "if the QName does not have a prefix, then the namespace URI is null" (XPath 1.0, section 2.3). A prefix in a query is expanded using the namespace declarations from the expression context, meaning you must supply them.

So there are two ways out: bind the namespace URI to a prefix of your choosing in the query context, or ignore namespaces with local-name(). The prefix you choose (a above) does not need to match anything in the document. Only the URI matters.

Python lxml:

from lxml import etree
root = etree.fromstring(xml_bytes)
ns = {"a": "http://www.w3.org/2005/Atom"}
root.xpath("//a:title", namespaces=ns)            # 3 elements
root.xpath("//a:link/@href", namespaces=ns)       # ['http://example.org/1']

You cannot map the default (empty) prefix in lxml: namespaces={None: uri} raised TypeError: empty namespace prefix is not supported in XPath. Python's standard ElementTree takes either Clark notation or a dict: root.findall('.//{http://www.w3.org/2005/Atom}title') and root.findall('.//a:title', ns) both returned 3, while .//title returned 0.

JavaScript (browser document.evaluate), tested in Chrome 156:

const doc = new DOMParser().parseFromString(xml, 'application/xml');
const resolver = (prefix) => (prefix === 'a' ? 'http://www.w3.org/2005/Atom' : null);
const r = doc.evaluate('//a:title', doc, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
r.snapshotLength;   // 3

Results in Chrome: //title with no resolver returned 0; //a:title with no resolver threw NamespaceError; and doc.createNSResolver(doc.documentElement) also threw NamespaceError for //a:title, because it only knows prefixes declared in the document and the default namespace has no prefix. You have to write the resolver function. If you do not need XPath at all, doc.getElementsByTagNameNS('http://www.w3.org/2005/Atom','title') returned 3, and so did getElementsByTagName('title') and querySelectorAll('title') (the DOM matches the local name for these).

C# (.NET 10):

var doc = new XmlDocument(); doc.Load("feed.xml");
var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("a", "http://www.w3.org/2005/Atom");
doc.SelectNodes("//a:title", ns);   // 3
doc.SelectNodes("//title");         // 0
doc.SelectNodes("//a:title");       // XPathException: Namespace Manager or XsltContext needed. This query has a prefix...

With LINQ to XML, x.Descendants("title") returned 0 and x.Descendants((XNamespace)"http://www.w3.org/2005/Atom" + "title") returned 3.

Fix 2: local-name()

//*[local-name()="title"]
//*[local-name()="title" and namespace-uri()="http://www.w3.org/2005/Atom"]

Both returned 3 in lxml and the first returned 3 in Chrome and .NET. It needs no setup, which makes it handy for a one-off in xmllint --xpath '...' feed.xml (not tested: the xmllint command line, though lxml uses the same libxml2). The cost is that it matches any namespace, so a title from another vocabulary (such as media:title) is also selected, unless you add the second condition. It is also slower to read.

Attributes and the URI

  • Unprefixed attributes have no namespace. Namespaces in XML (section 6.2) says default declarations "do not apply directly to attribute names". In lxml the <link> element's tag was {http://www.w3.org/2005/Atom}link while its attribute key was plain href, and in Chrome the attribute's namespaceURI was null. So //a:link/@href returned the value and //a:link/@a:href returned nothing.
  • The URI is just a name. A parser never fetches http://www.w3.org/2005/Atom; the spec says it is "not a goal" for the name to be usable for retrieving a schema. Two documents match when the URI strings are identical, so http://example.com/ns and http://example.com/ns/ are different namespaces.

XPath 1.0 essentials, tested on the same feed

GoalExpression (with a: bound)Result
second entry's title/a:feed/a:entry[2]/a:titleSecond
last entry//a:entry[last()]/a:titleSecond
entries that have a link//a:entry[a:link]/a:titleFirst
countcount(//a:entry)2
text filter//a:title[contains(text(),"Sec")]Second
parents of link//a:link/ancestor::*feed, entry
all siblings after.../following-sibling::*the later siblings

Predicates go in [...], // means any depth, @name selects an attribute, and an axis such as ancestor:: or following-sibling:: walks the tree. XPath 2.0 and later add *:title to match any namespace, but those engines are not what lxml, browsers or .NET run. Not tested: *:title.

Check your document first

If you cannot tell which namespaces a file declares, format it with the XML Formatter and read the root element's xmlns attributes. Converting to JSON hides the problem, because the prefixes end up inside the key names. The XML Validator checks that the file is well-formed before you debug the query.

Tested

2026-11-02: Python 3.11.15, lxml 6.1.3, Chrome 156.0.8078.48 via playwright-core, .NET 10.0.10. W3C Namespaces in XML and XPath 1.0 wording checked against the specs.

Related tools

Written by Mian Ali Khalid. More guides in Data & Format.