Tim TerlegÄrd
2013-11-22 10:46:00 UTC
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());
}
}
}
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());
}
}
}