I wanted some info related to xpath parsing of default namespace using saxon. I am using the Saxon-HE-9.5.1-3.jar to use xpath 2 features in my code. After including the saxon library in the class path, I am facing a issue parsing xpath for XML documents with default namespaces.
Sample XML in am using :
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE xmlns="http://www.abc.com/" responseCode="200">
<HEADER>
<HITS>100</HITS>
</HEADER>
</RESPONSE>
Valid XPATH: /RESPONSE/HEADER/HITS
Below are the cases where it works and does not work:
"XPATH Works" : When no namespace is specified
Example :<RESPONSE responseCode="200">
"XPATH Works" : When namespace with prefix is given
Example :<RESPONSE xmlns:res="http://www.abc.com/" responseCode="200">
"XPATH does not work" : When default namespace with out prefix is given Example :
<RESPONSE xmlns="http://www.abc.com/" responseCode="200">
Can you please help me out on why saxon treats no namespace and default namespace in a different way ? Also how do I solve the case of doing xpath for documents with default namespace.
Below is chunks of my code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import net.sf.saxon.xpath.XPathEvaluator;
import net.sf.saxon.xpath.XPathFactoryImpl;
.
.
.
DocumentBuilder builder;
Document doc;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(b, 0, size));
XPathFactory factory = XPathFactoryImpl.newInstance(XPathConstants.DOM_OBJECT_MODEL);
XPathEvaluator xpathCompiler = (XPathEvaluator) factory.newXPath();
XPathExpression expr = xpathCompiler.compile(xpath);
NodeList childNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Thanks & Regards Pratap