I am trying to do some searches on the "correct" pom.xml used by maven. For this, I use basic XPath queries from the JDOM.
Unfortunately, queries do not return any results (and do not have simple descendant filters). I am sure the problem is with the root pom.xml declaration:
<?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">
</project>
As you can see, an empty namespace is defined that does not match either "", or "http://www.w3.org/2000/xmlns/"where "" is the default namespace and the xmlns namespace is the default xmlns namespace.
So given a Documentwhen I want to run XPath-Query, as shown below:
XPathBuilder<Element> depQueryBuilder = new XPathBuilder<>("//dependencies/dependency", Filters.element());
XPathExpression<Element> depQuery = depQueryBuilder.compileWith(XPathFactory.instance());
for (Element elem : depQuery.evaluate(document)) {
}
, XPath ( XPathHelper javadoc), , , .
:
depQueryBuilder.setNamespace("", document.getRootElement().getAttributeValue("xmlns"));
depQueryBuilder.setNamespace("", "http://maven.apache.org/POM/4.0.0");
depQueryBuilder.setNamespace(Namespace.NO_NAMESPACE);
depQueryBuilder.setNamespace(document.getRootElement().getNamespace("xmlns"));
depQueryBuilder.setNamespace(document.getRootElement().getNamespace(""));
depQueryBuilder.setNamespace("xmlns", "http://maven.apache.org/POM/4.0.0");
, . XPath ?
. :
document.getRootElement().getDescendants(Filter.element("dependency"));
document.getRootElement().getChild("dependencies").getChildren("dependency"));