Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behavior of the != XPath operator

I'm attempting to create an xsl:choose statement with multiple conditions to test. So far, I have this:

<xsl:choose>     <xsl:when test="$AccountNumber != '12345' and $Balance != '0'">        <do stuff here>        ... 

The problem is that the 'and' is being treated as an 'or'. If the account number is 12345 or the balance of an account is 0, the condition is treated as true and the code gets executed. I need the test to be that both conditions must be true... do I have the syntax wrong here?

Thanks in advance, ~Tim

like image 986
user2191439 Avatar asked Mar 20 '13 15:03

user2191439


1 Answers

The problem is that the 'and' is being treated as an 'or'.

No, the problem is that you are using the XPath != operator and you aren't aware of its "weird" semantics.

Solution:

Just replace the any x != y expressions with a not(x = y) expression.

In your specific case:

Replace:

<xsl:when test="$AccountNumber != '12345' and $Balance != '0'"> 

with:

<xsl:when test="not($AccountNumber = '12345') and not($Balance = '0')"> 

Explanation:

By definition whenever one of the operands of the != operator is a nodeset, then the result of evaluating this operator is true if there is a node in the node-set, whose value isn't equal to the other operand.

So:

 $someNodeSet != $someValue 

generally doesn't produce the same result as:

 not($someNodeSet = $someValue) 

The latter (by definition) is true exactly when there isn't a node in $someNodeSet whose string value is equal to $someValue.

Lesson to learn:

Never use the != operator, unless you are absolutely sure you know what you are doing.

like image 184
Dimitre Novatchev Avatar answered Sep 23 '22 13:09

Dimitre Novatchev