Unprefixed names in XPath always mean "no namespace" - they do not respect the default namespace declaration. You need to use the prefix
Element rootElem = new Builder().build(xml).getRootElement(); xc = XPathContext.makeNamespaceContext(rootElem); xc.addNamespace("ex", "http://www.edankert.com/examples/"); Nodes matchedNodes = rootElem.query("ex:cd/ex:artist", xc); System.out.println(matchedNodes.size());
It doesn’t matter that the XPath expression uses a prefix that was not the original document if the namespace URI associated with the prefix in the context of the XPath namespace matches the URI that is associated with xmlns in the document.
source share