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
Saxonica

On 22 Nov 2013, at 10:46, Tim Terlegård <tim.terlegard@gmail.com> wrote:

Hi,

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?

Thanks,
Tim


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;
import java.io.StringReader;
import javax.xml.transform.stream.StreamSource;

public class Test {
   public static void main(String[] args) throws Exception {
      String xml = "<a xmlns=\"http://example.com/schema1\">" +
                   "  <b>Testing</b>" +
                   "  <c xmlns=\"http://example.com/schema2\">" +
                   "    <d>Again</d>" +
                   "  </c>" +
                   "</a>";

      Processor processor = new Processor(false);

      DocumentBuilder builder = processor.newDocumentBuilder();
      XdmNode metadataNode = builder.build(new StreamSource(new StringReader(xml)));
      XPathCompiler xpathCompiler = processor.newXPathCompiler();
      xpathCompiler.declareNamespace("", "http://example.com/schema1");
      xpathCompiler.declareNamespace("", "http://example.com/schema2");
      XPathSelector selector = xpathCompiler.compile("/a/c/d/text()").load();
      selector.setContextItem(metadataNode);

      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.
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help