You are 99% of the way there, you just need to make your NamespaceContext
return the right thing for the xsi
prefix:
public String getNamespaceURI(String prefix) {
String uri = null;
if ("n".equals(prefix)) {
System.out.println("using prefix");
uri = nonameNamespace;
} else if ("xsi".equals(prefix)) {
uri = "http://www.w3.org/2001/XMLSchema-instance";
}
return uri;
}
Also, if you are creating the domDoc
by parsing XML using a DocumentBuilder
then you must ensure that the builder is namespace-aware by calling docBuilderFactory.setNamespaceAware(true)
before .newDocumentBuilder()
. For what I can only presume are historical reasons, the default for DBF is to parse without handling namespaces, and such a DOM tree won't work with XPath.