954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I tell Java to return a zero in case a boolean XPath expression returns false?

I have the following XPath expression written in Java:

" pProbs = XPath.newInstance(/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");


My problem is that not all of the contains() expressions return true. What I need is:

If one of them returns 'false' (i.e.: there are no nodes containing pattern 'x y z'), then Java must output a '0'. Seems simple enough, but I can't come up with a solution for this.

Any ideas?

Thanks

uid1
Newbie Poster
5 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Use "and" instead of "or"

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Yeah that could be a solution, but it won't give me a collection of true and false, right?

I fixed this a few days ago by assigning each contains() to its own List. That way, if the result contains nothing, it returns a zero-length array and I can work with that.

Solution:

pProbs1 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+")]")
pProbs2 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content2+")]")
etc.


pProbs1 returned a zero-length array because its evaluation of contains() resulted in 'false'. :)

uid1
Newbie Poster
5 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: