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).
Related
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')" />
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 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>
I have an xml like, values can be
<n1>value1</n1>
<n1>value1</n1>
<n1>value2</n1>
I need to check if all these values are same and if same I would need to assign it to another element. I am using XSLT v1.0.
Thanks,
Good question, +1.
Just use:
not(/*/n1[1] != /*/n1)
Assuming that all n1 elements are selected in a variable named $v, this can be expressed in just 14 characters-long XPath expression:
not($v[1] != $v)
Explanation 1:
By definition:
/*/n1[1] != /*/n1
is true() exactly if there exists a node in /*/n1 whose string value isn't equal to the string value of /*/n1[1]
The logical negation of this:
not(/*/n1[1] != /*/n1)
is true() iff no node in /*/n1 exists whose string value isn't equal to the string value of /*/n1[1] -- that is, if all nodes in /*/n1 have the same sting value.
Explanation 2:
This follows from a more general double negation law :
every x has property y
is equivalent to:
There is no x that doesn't have property y
Assume a document of this form:
<root>
<n1>value1</n1>
<n1>value1</n1>
<n1>value1</n1>
</root>
The following simple stylesheet determines whether every n1 element has the same value:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="
concat('All same? ', count(/*/n1[.=/*/n1[1]])=count(/*/n1))"/>
</xsl:template>
</xsl:stylesheet>
Output:
All same? true
The key to this stylesheet is the expression:
count(/*/n1[.=/*/n1[1]])=count(/*/n1))
...which compares the count of the n1 elements whose value equals the value of the first n1 element to the count of all n1 elements. These counts will be equal only when every n1 node has the same value.
This can be made a little easier to read by first selecting all n1 into a variable named n:
count($n[.=$n[1]])=count($n)
Conditionally perform some action based on the result like this:
<xsl:template match="/">
<xsl:variable name="all" select="count(/*/n1[.=/*/n1[1]])=count(/*/n1)"/>
<xsl:if test="$all">All same</xsl:if>
<xsl:if test="not($all)">Not all same</xsl:if>
</xsl:template>