Discussion:
[saxon] XPath and nested default namespaces
Tim TerlegÄrd
2013-11-22 10:46:00 UTC
Permalink
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());
}
}
}
Michael Kay
2013-11-22 12:50:48 UTC
Permalink
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
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/
https://lists.sourceforge.net/lists/listinfo/saxon-help
Loading...