0
votes

I am evaluating the below XPath Boolean expression using Java's XPath API. However, the expression always returns true.

boolean(//InteractionControl/MessageId[text() = "fdsfsd"])

The text value of the MessageId node in the XML document is "9000," so the above XPath expression should return false unless I am missing something.

2
If count(//InteractionControl/MessageId[text() = "fdsfsd"]) is not 0, than your xpath matches non-empty Node set - Ivan Pronin
@IvanPronin That should be an answer, not a comment. - Jim Garrison
@Jim Garrison, thanks, added the answer - Ivan Pronin
How many MessageId elements are in your document? It would be helpful if you posted an example XML document that is producing the unexpected results. - Mads Hansen

2 Answers

3
votes

If count(//InteractionControl/MessageId[text() = "fdsfsd"]) is not 0, than your xpath matches non-empty Node set, which resolves boolean() function to true. See here: https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/boolean

0
votes

I resolved the problem by using the "evaluate" function in the XPath API which takes an argument specifying a return type.

Boolean evaluation = xPath.compile(xPathStr).evaluate(xmlDocument, XPathConstants.BOOLEAN)

Maybe the "evaluate" function that does not take an argument specifying the return type has a bug.