Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath operator "!=". How does it work?

XML document:

<doc>     <A>            <Node>Hello!</Node>        </A>       <B>              <Node/>     </B>        <C>     </C>      <D/> </doc> 

How would you evaluate the following XPath queries?

/doc/A/Node != 'abcd'   /doc/B/Node != 'abcd'   /doc/C/Node != 'abcd'   /doc/D/Node != 'abcd'   

I would expect ALL of these to evaluate to true.

However, here are the results:

/doc/A/Node != 'abcd'     true /doc/B/Node != 'abcd'     true /doc/C/Node != 'abcd'     false /doc/D/Node != 'abcd'     false 

Is this expected behavior? Or is it a bug with my XPath provider (jaxen)?

like image 282
Keith Avatar asked Jan 07 '11 19:01

Keith


People also ask

How do you write not equal to in XPath?

Format: != If the expression is not equal to the specified value, the operator returns true. If the expression is equal to the specified value, the operator returns false. Example: price!=

How write XPath with and operator?

The "and " operator is used to combining two different conditions or attributes to identify any element from a webpage using XPath efficiently. For example, if we have two attributes, a and b, we can combine both to uniquely identify an element on the webpage using the "and " operator.


1 Answers

Recommendation: Never use the != operator to compare inequality where one or both arguments are node-sets.

By definition the expression:

$node-set != $value 

evaluates to true() exactly when there is at least one node in $node-set such that its string value is not equal to the string value of $value.

Using this definition:

$empty-nodeset != $value  

is always false(), because there isn't even a single node in $empty-nodeset for which the inequality holds.

Solution:

Use:

not($node-set = $value) 

Then you get all results true(), as wanted.

like image 94
Dimitre Novatchev Avatar answered Sep 24 '22 15:09

Dimitre Novatchev