Question

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 .

Was it helpful?

Solution

Try this :

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top