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

Use "and" instead of "or"

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<Element>. 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'. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.