4

i have class with the members -

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

i try to use evaluate function -

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

when expression is a string s.t

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

and i get the follow exception -

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

even when i change XPathConstants.NODESET to XPathConstants.NUMBER i get same exception . thanks in advance .

URL87
  • 10,667
  • 35
  • 107
  • 174

2 Answers2

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

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

Try this :

expression = "inventory/book[" +
         "count(following-sibling::book[price/text()=14.95])=0]";
Arnaud
  • 7,259
  • 10
  • 50
  • 71