How does XPath deal with XML namespaces?

Defining namespaces in XPath (recommended)

XPath itself doesn’t have a way to bind a namespace prefix with a namespace. Such facilities are provided by the hosting library.

It is recommended that you use those facilities and define namespace prefixes that can then be used to qualify XML element and attribute names as necessary.

Here are some of the various mechanisms which XPath hosts provide for specifying namespace prefix bindings to namespace URIs.

(OP’s original XPath, /IntuitResponse/QueryResponse/Bill/Id, has been elided to /IntuitResponse/QueryResponse.)


XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("i", "");
XmlNodeList nodes = el.SelectNodes(@"/i:IntuitResponse/i:QueryResponse", nsmgr);

Java (SAX):

NamespaceSupport support = new NamespaceSupport();
support.declarePrefix("i", "");

Java (XPath):

xpath.setNamespaceContext(new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
      switch (prefix) {
        case "i": return "";
        // ...
  • Remember to call
  • See also:
    Java XPath: Queries with default namespace xmlns


See Implementing a User Defined Namespace Resolver:

function nsResolver(prefix) {
  var ns = {
    'i' : ''
  return ns[prefix] || null;
document.evaluate( '/i:IntuitResponse/i:QueryResponse', 
                   document, nsResolver, XPathResult.ANY_TYPE, 
                   null );

Note that if the default namespace has an associated namespace prefix defined, using the nsResolver() returned by Document.createNSResolver() can obviate the need for a customer nsResolver().

Perl (LibXML):

my $xc = XML::LibXML::XPathContext->new($doc);
$xc->registerNs('i', '');
my @nodes = $xc->findnodes('/i:IntuitResponse/i:QueryResponse');

Python (lxml):

from lxml import etree
f = StringIO('<IntuitResponse>...</IntuitResponse>')
doc = etree.parse(f)
r = doc.xpath('/i:IntuitResponse/i:QueryResponse', 

Python (ElementTree):

namespaces = {'i': ''}
root.findall('/i:IntuitResponse/i:QueryResponse', namespaces)

Python (Scrapy):

response.selector.register_namespace('i', '')


Adapted from @Tomalak’s answer using DOMDocument:

$result = new DOMDocument();

$xpath = new DOMXpath($result);
$xpath->registerNamespace("i", "");

$result = $xpath->query("/i:IntuitResponse/i:QueryResponse");

See also @IMSoP’s canonical Q/A on PHP SimpleXML namespaces.

Ruby (Nokogiri):

puts doc.xpath('/i:IntuitResponse/i:QueryResponse',
                'i' => "")

Note that Nokogiri supports removal of namespaces,


but see the below warnings discouraging the defeating of XML namespaces.


xmlNS = "xmlns:i=''"
doc.setProperty "SelectionNamespaces", xmlNS  
Set queryResponseElement =doc.SelectSingleNode("/i:IntuitResponse/i:QueryResponse")


xmlDoc = New XmlDocument()
nsmgr = New XmlNamespaceManager(New XmlNameTable())
nsmgr.AddNamespace("i", "");
nodes = xmlDoc.DocumentElement.SelectNodes("/i:IntuitResponse/i:QueryResponse",

SoapUI (doc):

declare namespace i='';


-N i=""


<xsl:stylesheet version="1.0"

Once you’ve declared a namespace prefix, your XPath can be written to use it:


Defeating namespaces in XPath (not recommended)

An alternative is to write predicates that test against local-name():


Or, in XPath 2.0:


Skirting namespaces in this manner works but is not recommended because it

  • Under-specifies the full element/attribute name.

  • Fails to differentiate between element/attribute names in different
    namespaces (the very purpose of namespaces). Note that this concern could be addressed by adding an additional predicate to check the namespace URI explicitly1:

     /*[    namespace-uri()='' 
        and local-name()='IntuitResponse']
     /*[    namespace-uri()='' 
        and local-name()='QueryResponse']

    1Thanks to Daniel Haley for the namespace-uri() note.

  • Is excessively verbose.

Leave a Comment