XPath explained
Why XPath returns no results with a default namespace
In XPath 1.0, an unprefixed element name selects elements in no namespace. If your XML declares a default namespace, an expression such as /catalog/book will not select those namespaced elements.
A small document that explains the problem
Consider a catalog from a supplier. The element names look familiar, but the default namespace gives catalog, book, and title a namespace URI. The id attribute remains unnamespaced.
<catalog xmlns="urn:example:books">
<book id="b1">
<title>Field Notes</title>
</book>
</catalog>
<!-- XPath 1.0: no matching elements -->
/catalog/book/titleUse a prefix bound to the namespace URI
In an XPath tool that exposes namespace bindings, bind a query prefix such as b to urn:example:books, then qualify each element step. The query prefix is a label for the URI; it need not appear in the original document.
Namespace-binding controls differ by tool. In XML Lens, inspect a node’s namespace and generated XPath first rather than assuming a particular manually entered prefix has been registered.
# With b bound to urn:example:books
/b:catalog/b:book/b:title
# Filter on the unnamespaced id attribute
/b:catalog/b:book[@id="b1"]/b:titleAn explicit expression without a prefix binding
For this example, match both the local name and the namespace URI. This is longer, but it makes the namespace requirement explicit without relying on a prefix configuration.
/*[local-name()="catalog" and namespace-uri()="urn:example:books"]
/*[local-name()="book" and namespace-uri()="urn:example:books"]
/*[local-name()="title" and namespace-uri()="urn:example:books"]- The expected result is one title element containing Field Notes.
- Use the URI exactly as declared in the document.
- Checking only local-name() can mix elements from different namespaces.
Debug one step at a time
Start at the root and add one path step at a time. If the first step matches and the next does not, inspect the next element’s name and namespace rather than changing the entire query.
Also check context: an absolute path begins at the document root, while a relative path starts from the current context node. A correct relative expression can return nothing when evaluated from the wrong place.
Save the working expression with a short note about its namespace assumptions. When a supplier changes its export format, that note helps you distinguish a data change from a query error.
Common questions
Should I remove xmlns to make the query work?
No. Removing a namespace changes the identity of the elements and may break validation or downstream processing. Adapt the query to the document.
Does a default namespace apply to an unprefixed attribute?
No. In the catalog example, id is unnamespaced even though book belongs to urn:example:books.
References
W3C XPath 1.0: node tests ↗W3C Namespaces in XML: namespace defaulting ↗Try the workflow in XML Lens
Explore the relevant Mac workspaces and their capabilities.
Query XML with XPath 1.0 on your Mac See any XML document as an interactive map View and format XML locally on Mac