Regarding xpath parsing of xml with default namespace using Saxon
java, namespaces, saxon, xml, xpath
Solution
This is not Saxon-specific but rather a fundamental part of how namespaces work. In your examples 1 and 2 the `RESPONSE` element is not in a namespace but in case 3 it (and all its descendants) are in the `http://www.abc.com/` namespace. With the `javax.xml.xpath` API you need to define a `NamespaceContext` if you want to be able to match nodes in a specific namespace, or since you're in XPath 2.0 you can use the `*:localName` notation to match all nodes with a given local name regardless of their namespace.
/*:RESPONSE/*:HEADER/*:HITS
Problem
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