It really doesn't matter whether namespaces in the source document are bound to one prefix, several prefixes, or no prefix; it's the namespace URI that counts, not the prefix or absence of a prefix.

In the XPath expression, the parser needs to know what expanded name each QName is referring to. So there can only be one set of prefix/uri bindings for the whole path expression, which means there can only be one binding for the "absent" prefix, that is, the XPath default element namespace.

Michael Kay

On 22 Nov 2013, at 10:46, Tim Terlegård <> wrote:


I'm trying to run an xpath on an xml with nested default namespaces. I'd rather not specify any namespaces in the xpath, is this possible?

I have made a minimal example below that shows what I'd like to do and obviously it doesn't work. Is it not possible to declare multiple namespaces with empty prefix?


import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;

public class Test {
   public static void main(String[] args) throws Exception {
      String xml = "<a xmlns=\"\">" +
                   "  <b>Testing</b>" +
                   "  <c xmlns=\"\">" +
                   "    <d>Again</d>" +
                   "  </c>" +

      Processor processor = new Processor(false);

      DocumentBuilder builder = processor.newDocumentBuilder();
      XdmNode metadataNode = StreamSource(new StringReader(xml)));
      XPathCompiler xpathCompiler = processor.newXPathCompiler();
      xpathCompiler.declareNamespace("", "");
      xpathCompiler.declareNamespace("", "");
      XPathSelector selector = xpathCompiler.compile("/a/c/d/text()").load();

      for (XdmItem xdmItem : selector.evaluate()) {
         System.out.println("Val = " + xdmItem.getStringValue());
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing
conversations that shape the rapidly evolving mobile landscape. Sign up now.
saxon-help mailing list archived at