Fernando Ferreira
Fernando Ferreira

Reputation: 808

XSLT conditional test with absolute value

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) &lt; /root/thresholds/min">

   <xsl:attribute name="style">background-color:red;</xsl:attribute>

</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?

Upvotes: 1

Views: 3583

Answers (3)

Michael Kay
Michael Kay

Reputation: 163458

Another way to find the absolute value of a number in XPath 1.0 is

number(translate(string($X), '-', ''))

Upvotes: 1

Dennis Kreminsky
Dennis Kreminsky

Reputation: 2089

Absolute as in non-negative? Define a variable that is conditional.

<xsl:variable name="abs">
   <xsl:choose>
       <xsl:when test="/root/values/mean &lt; 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> 

Upvotes: 2

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

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()

Upvotes: 3

Related Questions