I'm trying to perform some retrieval queries on a "correct" pom.xml used by maven. For that I use basic XPath queries from JDOM.
Unfortunately the queries do not return any results (and neither do simple descendant filters). I'm reasonably sure that the problem lies within the root declaration of the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!-- content -->
</project>
As can be seen an empty-namespace is defined that doesn't match either ""
or "http://www.w3.org/2000/xmlns/"
, where "" is the default No-Namespace and the xmlns namespace is the default xmlns namespace.
So given a Document
, when I want to perform an XPath-Query like follows:
XPathBuilder<Element> depQueryBuilder = new XPathBuilder<>("//dependencies/dependency", Filters.element());
XPathExpression<Element> depQuery = depQueryBuilder.compileWith(XPathFactory.instance());
for (Element elem : depQuery.evaluate(document)) {
// basically unreachable, since the resultset is always empty
}
Given the fact that all XPath expressions and queries are required to be namespace aware (compare the XPathHelper javadoc), I'm pretty sure that I can get this to work by adding the required namespace declarations.
I've tried the following with different kinds of failure:
depQueryBuilder.setNamespace("", document.getRootElement().getAttributeValue("xmlns"));
// NPE: Null URI
depQueryBuilder.setNamespace("", "http://maven.apache.org/POM/4.0.0");
// Cannot set a Namespace URI in XPath for "" prefix
depQueryBuilder.setNamespace(Namespace.NO_NAMESPACE);
// no error-message, but no results either
depQueryBuilder.setNamespace(document.getRootElement().getNamespace("xmlns"));
// NPE: Null Namespace
depQueryBuilder.setNamespace(document.getRootElement().getNamespace(""));
// Cannot set a Namespace URI in XPath for "" prefix
depQueryBuilder.setNamespace("xmlns", "http://maven.apache.org/POM/4.0.0");
// Name "xmlns" is not legal for JDOM/XML Namespace prefix
At this point I'm not even sure I'm attacking this at the right point. How can I get my XPath query to return results?
Note: The following more simple queries don't return results either:
document.getRootElement().getDescendants(Filter.element("dependency"));
// empty iterator
document.getRootElement().getChild("dependencies").getChildren("dependency"));
// NullPointerException because there is no child "dependencies"