I have the following code that leads to unexpected results:
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("""
<ns:Msg xmlns:ns=\"abc\">
<ns:MsgType>
<ns:MsgType>TestType</ns:MsgType>
</ns:MsgType>
<ns:MsgType>
<ns:MsgType>TestType</ns:MsgType>
</ns:MsgType>
</ns:Msg>
""")));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodelist = (NodeList) xPath.compile("//MsgType/MsgType").evaluate(document, XPathConstants.NODESET);
nodelist.getLength(); // => 2 ok
nodelist = (NodeList) xPath.compile("//MsgType").evaluate(document, XPathConstants.NODESET);
nodelist.getLength(); // => 0 why?
How can //MsgType/MsgType return more nodes than //MsgType?
(The DomBuildingFactoy's namespaceAware default property is set to false by default.
Omitting the namespace ns and the namespace declaration altogether returns the expected results: 2 and 4 respectively.)
The effect of running XPath against a non-namespace-aware DOM is undefined. The XPath language spec assumes namespace-awareness in the XML. Of course, if the DOM is namespace-aware then you need to write the XPath to take account of the namespaces.
I agree that the observed effect is surprising, however I don't think it's a bug.