4

我和成员们一起上课-

private Document myDoc;
XPath xpath = XPathFactory.newInstance().newXPath(); 

我尝试使用evaluate功能 -

Object result = xpath.evaluate(expression, this.myDoc,
                XPathConstants.NODESET);

什么时候expressionstringst

expression = "inventory/book[" +
                "count(following-sibling::book/price/text()=14.95)=0 ]"  ;

我得到以下异常 -

java.lang.RuntimeException: Can not convert #BOOLEAN to a NodeList!

即使我更改XPathConstants.NODESETXPathConstants.NUMBER我得到相同的异常。提前致谢 。

4

2 回答 2

2

This is one of the few cases where XPath 1.0 returns a type error: the argument to the count() function is a boolean expression (path/a/b = 14.94), and count() requires it to be a node-set. (In XPath 2.0 the count of a single boolean is 1, so your query would run successfully, and give wrong answers).

于 2012-05-22T13:01:43.407 回答
1

尝试这个 :

expression = "inventory/book[" +
         "count(following-sibling::book[price/text()=14.95])=0]";
于 2012-05-22T09:53:46.227 回答