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'))">
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 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')" />
This Java program makes use of a Ternary if, to map booleans to output strings: (a "*" for true, an empty string for false).
public class ternary {
public static void main(String[] args) {
boolean flags[]={true,false,true};
for (boolean f : flags) {
System.out.println(f?"*":"");
}
}
}
So the output is *, [empty], *.
I have an input XML document, something like:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="change.xsl"?>
<root>
<change flag="true"/>
<change flag="false"/>
<change flag="true"/>
</root>
And I have the following XSLT template which maps true to '*' and false to '' (it works):
<xsl:template match="change">
<xsl:variable name="flag" select="translate(substring(#flag,1,1),'tf','*')"/>
<xsl:value-of select="$flag"/>
</xsl:template>
Is there is more concise version of this ?
a) Can I automatically get a value of boolean true|false directly from the string 'true|false' ?
b) Is there an (xpath?) construct to map the boolean true|false to '*', '' ?
a) Can I automatically get a value of boolean true|false directly from
the string 'true|false' ?
b) Is there an (xpath?) construct to map the boolean true|false to
'*', '' ?
This is actually an XPath question.
I. XPath 1.0
There are more than one XPath expressions the evaluation of which produces the wanted result:
substring('*', 2 -(#flag = 'true'))
This also answers b) -- notice that the strings 'true' and 'false' aren't boolean values! The only two boolean values are true() and false() and they are not strings.
In the above expression we use the fact that in XPath 1.0 if a boolean is in a context where a number is needed, it is automatically converted to number. By definition:
number(true()) is 1
and
number(false()) is 0
Thus the second argument of the call to substring() above:
2 - (#flag = 'true')
is evaluated as 1 when #flag = 'true' and to 2 otherwise.
A more general XPath 1.0 expression that produces the string $s1 if $val is "x" and produces the string $s2 if $val is "y" :
concat(substring($s1, 1 div ($val = "x")),
substring($s2, 1 div ($val = "y"))
)
This produces the string $s1 when $val = "x", the string $s2 when $val = "y", and the empty string -- if none of these two conditions holds.
The above XPath 1.0 expression can be generalized to produce N different string results $s1, $2, ..., $sN exactly when $val is one of the values $v1, $v2, ..., $vN because the function concat() can have any number of arguments.
II. XPath 2.0 (XSLT 2.0)
'*'[current()/#flag = 'true']
And more generally, given 2*N atomic values $s1, $s2, ... $sN and $v1, $v2, ..., $vN, such that all $vi values are different, the result of evaluating this XPath 2.0 expression:
($s1, $s2, ..., $sN)[index-of(($v1, $v2, ..., $vN), $v)]
is $sK exactly when $v eq $vK.
You could utilise simple pattern matching in templates for this.
<xsl:template match="change[#flag = 'true']">
<xsl:template match="change">
So, the first one matches true entries, and the other one matches all other cases (which in your cases is just false
So, given the following stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="change[#flag = 'true']">
<xsl:text>*
</xsl:text>
</xsl:template>
<xsl:template match="change">
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:apply-templates select="#*|node()"/>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is output
*
*
I have this xslt :
<xsl:param name="total_articles" select="3" />
<xsl:param name="articles_per_page" select="3" />
<xsl:apply-templates select="dagboek/entry[position > $offset][position < $articles_per_page+$offset]" >
<xsl:with-param name="total_pages" tunnel="yes">
<xsl:choose>
<xsl:when test="$value="2005-09" and $page="1">8</xsl:when>
<xsl:otherwise>floor(number($total_articles)-1) div $articles_per_page +1</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:apply-templates>
But now I get this error.
How can I calculate the outcome of the calculation and put it into the param total_pages.
Roelof
Edit 1: WHat I try to achieve is that if it's not 2005-09 and page is not 1 then the totalpages is calculated out of total_articles and articles_per_page. The outcome has to be put into the param pages.
<xsl:when test="$value="2005-09" and $page="1">8</xsl:when>
This isn't even well-formed XML document. The problem is with the nested quotes.
Probably you meant:
<xsl:when test="$value='2005-09' and $page='1' ">8</xsl:when>
Another possible problem: Tunnel parameters are only available in XSLT 2.0. You seem to be using XSLT 1.0
Update:
The OP in a comment has modified/clarified his initial question:
WHat I try to achieve is that if it's not 2005-09 and page is not 1
then the totalpages is calculated out of total_articles and
articles_per_page. The outcome has to be put into the param pages.
This can simply be expressed as:
<xsl:with-param name="total_pages" select=
"8*($value='2005-09' and $page='1')
+
(floor(number($total_articles)-1) div $articles_per_page +1)
*
not($value='2005-09' and $page='1')
"/>
Explanation:
We are using the fact that in XPath 1.0 by definition number($someBoolean) is 1 if $someBoolean is true() and is 0 if $someBoolean is false().
Therefore the pseudocode:
if($vCond)
then $vNum1
else $vNum2
can be expressed with a single XPath expression:
$vNum1*$vCond + $vNum2*not($vCond )
whenever a boolean is an argument to an arithmetic operator, it is automatically converted to a number.
So, here's what happens at run-time:
Suppose $vCond is true(), therefore not($vCond) is false().
Because $vCond and not($vCond) are arguments to the * operator, they are converted to numbers, respectively 1 and 0:
...
$vNum1*1 + $vNum2*0
This is equivalent to:
...
$vNum1*1
Note:
The above equivalence rule can be further generalized to N mutually exclusive conditions $vCond1, $vCond2, ..., $vCondN, and corresponding N values: $val1, $val2, ..., $valN:
$val1*$vCond1 + $val2*$vCond2 + ... + $valN*$vCondN
is equal to $valK (k in {1,..., N}) exactly when $vCondK is true()
You have a badly formed attribute:
<xsl:when test="$value="2005-09" and $page="1">8</xsl:when>
It should probably look like:
<xsl:when test="$value='2005-09' and $page='1'">8</xsl:when>
Is it this line?
<xsl:when test="$value="2005-09" and $page="1">8</xsl:when>
You're nesting double quotes - that's not going to work. Use a combination of single and double quotes :
<xsl:when test='$value="2005-09" and $page=1'>8</xsl:when>
Is there an exclusive OR 'XOR' in XPath1.0 ?
Use this XPath 1.0 expression:
x and not(y) or y and not(x)
Always try to avoid the != operator, because it has an unexpected meaning/behavior when one or both of its arguments are node-sets.
In XSLT 2.0 or XQuery 1.0 one can write this as a function and then use just the function in any XPath expression. Below is an XSLT 2.0 function definition for xor and a small example of using this function:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="my:f">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"for $x in (true(), false()),
$y in (true(), false())
return
('xor(', $x, ',', $y,') = ', f:xor($x, $y), '
')
"/>
</xsl:template>
<xsl:function name="f:xor">
<xsl:param name="pX" as="xs:boolean"/>
<xsl:param name="pY" as="xs:boolean"/>
<xsl:sequence select=
"$pX and not($pY) or $pY and not($pX)"/>
</xsl:function>
</xsl:stylesheet>
when this transformation is applied on any XML document (not used), the wanted, correct result is produced:
xor( true , true ) = false
xor( true , false ) = true
xor( false , true ) = true
xor( false , false ) = false
No but you can emulate it:
(a or b) and (a != b)
number($boolean_var) converts true() to 1 and false() to 0. (Note that true alone addresses a node!!)
boolean($numeric_var) converts 1 to true() and 0 to false().
Therefore, XOR can be accomplished by:
boolean((number($v1) + number($v2) + number($v3)) mod 2)
i.e. least-significant-bit addition using the mod 2 operator. Yes, XPATH is cumbersome.