JDOM2 and XPath

614 Views Asked by At

When I run the following

XPathFactory xpathFactory = XPathFactory.instance();
XPathExpression<Double> query =
xpathFactory.compile("count(1)", Filters.fdouble());
List<Double> result = query.evaluate(new Element("test"));

yet the evaluation fails:

Unable to evaluate expression. See cause Cause: Function :count

That is a bit nonsense example, but I wanted something self contained. What is wrong with this?

1

There are 1 best solutions below

1
On

If you want to count the number of test elements, try this;

Element root = new Element("test");
Document doc = new Document(root);

XPathFactory xpathFactory = XPathFactory.instance();
XPathExpression<Double> query = xpathFactory.compile("count(//test)", Filters.fdouble());
List<Double> result = query.evaluate(doc);