JDOM2和XPath

时间:2013-09-13 07:42:27

标签: java xml xpath jdom

当我运行以下

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

然而评估失败了:

无法评估表达式。看原因 原因:功能:计数

这是一个无意义的例子,但我想要一些自包含的东西。这有什么问题?

1 个答案:

答案 0 :(得分:1)

如果您想计算test元素的数量,请尝试此操作;

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);