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
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.
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.
I've always used this syntax, which yields more predictable results than using !=.
<xsl:when test="not($AccountNumber = '12345') and not($Balance = '0')" />
Related
I definded the following variables:
<xsl:variable name="pica036E"
select="recordData/record/datafield[#tag='036E']" />
<xsl:variable name="pica036F"
select="recordData/record/datafield[#tag='036F']" />
Now I need to do a condition if variable pica036E isn't empty and pica036F is empty show the following message otherwise show another message.
That's my code, but I don't ge any output. Is "null or empty" correct defined?
<xsl:choose>
<xsl:when test="$pica036E != '' and $pica036F = ''">
<xsl:message>
036F no 036E yes
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:message>
036E no 036F yes
</xsl:message>
</xsl:otherwise>
</xsl:choose>
In XPath, X=Y means (if some pair x in X, y in Y satisfy x = y), while X != Y means (if some pair x in X, y in Y satisfy x != y).
This means that if either X or Y is an empty sequence, then both X=Y and X!=Y are false.
For example, $pica036E != '' tests whether there is a value in $pica036E that is not a zero-length string. If there are no values in $pica036E then there is no value that satisfies this condition.
As a result, using != in XPath is always a code smell. Usually, rather than X != Y, you should be writing not(X = Y).
Check following Code. I think your output get
<xsl:when test="not($pica036E = '') and $pica036F = ''">
In XSLT a variable with text content can also serve as a boolean variable.
Not empty content means true, empty content means false.
So the condition can be also written as:
<xsl:when test="$pica036E and not($pica036F)">
Remember that not is a function (not an operator).
I'm finding that while this test returns true:
test="string-length(foo) = 0"
However, for some reason, both of these tests are returning false:
test="foo = ''"
test="foo = null"
Any idea what might be going on? Is there some other state that foo could be in that would result in a 0 length, while still not being equal to '' or null?
Additionally - if I output:
X<xsl:value-of select="foo" />X
outputs: XX
If foo is an empty node-set then string(foo) is "", and string-length(foo) is zero, but foo = '' is false. XPath is full of surprises.
The explanation is that foo = "" doesn't mean string(foo) = "" as you might expect, but rather some $F in foo satisfies $F = "" (which you can write in full in XPath 2.0). And if foo is an empty set then the existential test is clearly false.
Well if you do test="foo = null" you compare the foo child element(s) of the context node to the null child element(s) of the context node.
I know I can sum over multiple nodes with numeric values.
How could I do a "boolean sum" over a set of nodes? For example:
<a>
<b>false</b>
<b>false</b>
<b>true</b>
<b>false</b>
</a>
How could I get the boolean OR of all the <b> node values? (which should be 'true').
Use:
boolean(/*/b[. = 'true'])
This produces the boolean value of the expression:
/*/b[. = 'true']
and is true exactly when the above expression selects at least one node -- that is, when there is a b that is a child of the top element and whose string value is the string 'true.
In case you want also to calculate the "boolean product" (using and), do:
not(/*/b[. = 'false'])
You could count the true values:
<xsl:if test="count(a/b[text()='true']) > 0">
true
</xsl:if>
If there are any true values the OR will be true.
I'm trying to display only limited characters in html td tag,following is my code,
if "VAR1" has less than 10 character,it's displaying '...' directly,how to check this out???
<td><xsl:value-of select="concat(substring(VAR1,1,10),'...')"/></td>
If you want to only show 10 characters, but only show the ... when there are more than 10 characters in the original string, you could do this in a single expression live so
<xsl:value-of select="concat(
substring(VAR1,1,10),
substring('...', 1 div (string-length(VAR1) > 10)))"/>
So, when VAR1 is '123456789' it will output just this
123456789
But when VAR1 is '123456789012' it will output this
1234567890...
To explain how this works, the following expression will either be true or false, depending on whether the length of the string is more than 10 or not
(string-length(VAR1) > 10)
When used in a numeric expression, true evaluates to 1, and false evaluates to 0. Now, in the case of the string being more than 10 characters in length, the full expression is evaluated like so
substring('...', 1 div (string-length(VAR1) > 10)))
= substring('...', 1 div true)
= substring('...', 1 div 1)
= substring('...', 1)
= '...'
So, with more than 10 characters, you get the '...' at the end.
However, when you have less than 10 characters, it evaluates like so
substring('...', 1 div (string-length(VAR1) > 10)))
= substring('...', 1 div false)
= substring('...', 1 div 0)
= substring('...', (A very big number!))
= ''
So, with less than 10 characters, the substring does not return anything.
if "VAR1" has less than 10 character,it should display '...' ? if this is your question, the following is the answer
<td>
<xsl:choose>
<xsl:when test='string-length(VAR1) > 10'>
<xsl:value-of select=concat(substring(VAR1,1,10),'...')/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="VAR1"/>
</xsl:otherwise>
</xsl:choose>
</td>
I'm trying to represent the following in an XSLT if:
if (status != Disconnected || status != BadNumber || status != NoCallsAccepted || status != Fax)
I know this can be done with a choose expression but i didn't want to add so much verbosity to the template...
I've tried different variations of the follow using both AND and OR when it appropriate:
<xsl:if test="not(#status = 'BadNumber') AND not(#status = 'Disconnected') AND not( #status = 'NoCallsAccepted') AND not(#status = 'Fax')">
But this leads to syntax errors.
This can be expressed in a more compact form as:
<xsl:if test=
"not(contains('|BadNumber|Disconnected|NoCallsAccepted|Fax|',
concat('|', #status,'|')
))">
In XPath 2.0 (XSLT 2.0) even a more compact expression is possible:
<xsl:if test="not(#status=('BadNumber','Disconnected','NoCallsAccepted','Fax'))">
I figured out my mistake... uppercase and.
<xsl:if test="#status != 'BadNumber' and #status != 'Disconnected' and #status != 'NoCallsAccepted'and #status != 'Fax'">
If you're using XSLT 2.0, you can simplify:
<xsl:if test="not(#status=('BadNumber','Disconnected','NoCallsAccepted','Fax'))">