I've recently hit a wall with an issue with Java's native XML API (the W3C one) which at its core is that if I try to use a direct XPath expression in my document like, say, //body the XPath is evaluated as false (incorrect behaviour), however if I replace that with //*[1] it evaluates as true (desired behaviour).
I have checked numerous times and with different documents that the XML I use is valid (I'm using mainly XHTML 2.0 with rest of this system).
Since I have no idea what really causes this, here's the set of configuration options for various factory objects I'm using.
As you can see, I have lots of custom implementations of various classes related to the whole issue which I think may have something to do with the issue but I'm not sure, running around with unit tests, playing with debugger and a huge load of .println:s hasn't helped me yet.
XPath:
xpathfactory.setXPathFunctionResolver(myFunctionResolver)xpath.setNamespaceContext(myNamespaceContext);xpath.setXPathVariableResolver(myResolver);
DocumentBuilder:
(f == DocumentBuilderFactory)
f.f.setValidating(false);f.setSchema(null);f.setNamespaceAware(true);f.setIgnoringComments(true);f.setIgnoringElementContentWhitespace(true);- +features
"http://xml.org/sax/features/validation","http://apache.org/xml/features/validation/schema"and"http://apache.org/xml/features/nonvalidating/load-external-dtd"asfalse
(dc == DocumentBuilder)
dc.setEntityResolver(null);dc.setErrorHandler(myErrorHandler);
Also worth mentioning is that I'm using Sun Java 5 on Windows XP.
All ideas are welcome at this point, I'm getting quite frustrated because of this issue.
Conclusion
It was a namespace issue, the problem was that I didn't declare default namespace in myNamespaceContext at all! Just by adding
else {
return "http://www.w3.org/1999/xhtml";
}
made the thing work and now I made it so that default namespace is detected. Works like a charm! Both answers helped me to find the cause and I'd select both as preferred answer if I could.