Reputation: 261
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
Upvotes: 26
Views: 115561
Reputation: 101652
If $AccountNumber
or $Balance
is a node-set, then this behavior could easily happen. It's not because and
is being treated as or
.
For example, if $AccountNumber
referred to nodes with the values 12345
and 66
and $Balance
referred to nodes with the values 55
and 0
, then
$AccountNumber != '12345'
would be true (because 66
is not equal to 12345
) and $Balance != '0'
would be true (because 55
is not equal to 0
).
I'd suggest trying this instead:
<xsl:when test="not($AccountNumber = '12345' or $Balance = '0')">
$AccountNumber = '12345' or $Balance = '0'
will be true any time there is an $AccountNumber
with the value 12345
or there is a $Balance
with the value 0
, and if you apply not()
to that, you will get a false result.
Upvotes: 8
Reputation: 243459
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.
Upvotes: 44
Reputation: 123
I've always used this syntax, which yields more predictable results than using !=.
<xsl:when test="not($AccountNumber = '12345') and not($Balance = '0')" />
Upvotes: 0