I'm trying to figure out how to make a test in my xsl transformation using absolute values. Something like this:
<xsl:when test="abs(/root/values/mean) < /root/thresholds/min">
<xsl:attribute name="style">background-color:red;</xsl:attrib开发者_JAVA技巧ute>
</xsl:when>
Is that possible. I've tried using templates, but it seemed the wrong path. Moving to XSLT 2.0 did not work for me either (I guess Firefox 3.6 do not support it).
Any thoughts?
Generally, in XPath 1.0 (XSLT 1.0) you can find the absolute value of a number $vNum
with the following XPath expression:
$vNum*($vNum > 0) -$vNum*not($vNum > 0)
In XPath 2.0 (XSLT 2.0) one uses the standard function abs()
Absolute as in non-negative? Define a variable that is conditional.
<xsl:variable name="abs">
<xsl:choose>
<xsl:when test="/root/values/mean < 0><xsl:value-of select="-1 * /root/values/mean" /></xsl:when>
<xsl:otherwise><xsl:value-of select="/root/values/mean" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:when test="$abs">
<xsl:attribute name="style">background-color:red;</xsl:attribute>
</xsl:when>
Another way to find the absolute value of a number in XPath 1.0 is
number(translate(string($X), '-', ''))
精彩评论