i have the following condition in my xslt 1.0
<xsl:when test="string-length(//Record/CIMtrek_CI_OPEX_200910_FrDiv/text()) != 0" ></xsl:when
but this condition is executed even if the value of CIMtrek_CI_OPEX_200910_FrDiv is 0,
i dont want this condition to be executed when the value of CIMtrek_CI_OPEX_200910_FrDiv is 0
how to do this in xslt 1.0
Please help me to get this done.
Best Regards
Use:
test="translate(//Record/CIMtrek_CI_OPEX_200910_FrDiv, '0', '')"
Related
Below The sample XSLT Code
<xsl:choose>
<xsl:when test="(TargetBonus >= 0)"><xsl:value-of select="TargetBonus"/></xsl:when>
<xsl:otherwise>Main</xsl:otherwise>
</xsl:choose>
Above logic I am getting Error like:
Cannot convert string "" to double
I am not able to find the exact issue
You have an empty <TargetBonus> element in your input. This will cause the test TargetBonus >= 0 to fail with a type conversion error.
Convert to number explicitly.
<xsl:when test="number(TargetBonus) >= 0]">...</xsl:when>
This will turn any invalid values (including the empty string) into NaN, and then the comparison will be possible. NaN >= 0 will result in false, which probably is what you expect.
I am having issues using the or operator in XSLT. I need to test if a variable does not equal 00 or 09.
The current value being entered into the XSLT for Timecodehour is 09. I am using the test below to confirm that the value must be 00 or 09.
<xsl:template match="/">
<xsl:if test="$TimecodeHour!='00' or $TimecodeHour!='09'">
<xsl:message terminate="yes">Timecode is not supported</xsl:message>
</xsl:if>
</xsl:template>
The test completes correctly if I removed "$TimecodeHour!='00' or" but submitted message Timecode is not supported when I add it back in again. Could someone please help me understand what I am doing wrong?
You need to use and not or here. or will be true if either of the conditions are true.
So, for example, if $TimecodeHour equals 09 then the expression $TimecodeHour!='00' returns true, and so the result of the or expression is true.
Using and will only return true if both conditions are true.
<xsl:if test="$TimecodeHour!='00' and $TimecodeHour!='09'">
I have to repeat the following XSLT snippet like 100 times and I would like it to be as small as possible. Is there a way to make an equivalent XSLT snippet that is shorter?
<xslo:variable name="myVariable" select="//This/that/anotherthing" />
<xslo:choose>
<xslo:when test="string($myVariable) != 'NaN'">
<xslo:text>1</xslo:text>
</xslo:when>
<xslo:otherwise>
<xslo:text>0</xslo:text>
</xslo:otherwise>
</xslo:choose>
I'm basically setting the state of a checkbox based on whether or not a value exists in //This/that/anotherthing in the source xml.
Can be XSLT 1.0 or XSLT 2.0, doesn't matter.
You can use an if instead of xsl:choose (XSLT 2.0 only)...
<xsl:value-of select="if (string(number(//This/that/anotherthing)) = 'NaN') then 0 else 1"/>
I also dropped the xsl:variable, but if you need it for some other reason, you can put it back.
You could also create a function...
<xsl:function name="local:isNumber">
<xsl:param name="context"/>
<xsl:value-of select="if (string(number($context)) = 'NaN') then 0 else 1"/>
</xsl:function>
usage...
<xsl:value-of select="local:isNumber(//This/that/anotherthing)"/>
<xslo:variable name="myVariable" select="//This/that/anotherthing" />
<xslo:value-of select="number(boolean($myVariable))"/>
If I understand the purpose correctly - that is return 1 if the value in question can be successfully expressed as a number, 0 otherwise - then I believe:
<xsl:value-of select="number(//This/that/anotherthing castable as xs:double)"/>
would be the most straightforward way (in XSLT 2.0) to achieve it.
Edit
In view of your change of purpose:
I'm basically setting the state of a checkbox based on whether or not
a value exists in //This/that/anotherthing
That's even simpler:
<xsl:value-of select="number(boolean(string(//This/that/anotherthing)))"/>
I have the below template as shown..
<xsl:for-each select="/abc/def">
<xsl:if test=".Id='xxx' and ./Role='yyy' ">
<xsl:value-of select=" 'true'"/>
</xsl:if>
Now I want to put an or condition in an xsl:if condition such that let say condition 1 is true then also result should be true or if condition 2 is true then also the result is true , now please advise how to put the or condition
I have come up with this solution please advise is it correct ....
<xsl:if test=".Id='xxx' and ./Role='yyy' or test=".Id='ttt' and ./Role='ccc' ">
Everytime I'm not sure about priorities I use brackets, so e.g. test="(Id='xxx' and Role='yyy') or (Id='ttt' and Role='ccc')".
Btw, there is only one "test" attribute in "if", you don't need to repeat it for second condition.
I was wondering whether there is a way to make a negative value, positive? Here is a snippet of my xslt document:
<NumberOfLinesAtRate>
<xsl:value-of select="number(/xsales:Qty)" />
</NumberOfLinesAtRate>
The problem is, sometimes this Qty value may be negative, -1, -2 etc.. in my original XML document. Is there a way I can make this always be positive in my transformed document?
If you are using XSLT1.0 you can use this formula (I've left out the namespace for brevity)
<xsl:value-of select="Qty * (Qty >= 0) - Qty * not(Qty >= 0)" />
In XSLT2.0 there is a dedicated abs operator you can use
<xsl:value-of select="abs(Qty)" />
This would be a fairly concise way to do it:
<xsl:value-of select="Qty * (1 - 2 * (Qty < 0))" />