Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the absolute value of a number in XSLT?

I have

<xsl:value-of select="DifferenceInDays" /> 

DifferenceInDays has a value that can be a negative or a positive number, I want to display its absolute value. How can I do this?

like image 234
Mithil Avatar asked Apr 29 '09 21:04

Mithil


People also ask

How do you do absolute value in XSLT?

In XPath 2.0 (XSLT 2.0) use the abs() function.

How do you escape a single quote in XSLT?

You can use the built-in entities &apos; and &quot; In XSLT 1.0: Alternatively, you can define your $Q and $APOS variables (put the content (the literal " or the literal ' character) in the body of the xsl:variable , not in the select attribute).


2 Answers

In XPath 1.0 use the following expression:

   $vNum*($vNum >=0) - $vNum*($vNum < 0)

In case this expression is embedded in an XSLT (XML) attribute, the < character must be escaped:

   $vNum*($vNum >=0) - $vNum*($vNum &lt; 0)

In XPath 2.0 (XSLT 2.0) use the abs() function.

like image 136
Dimitre Novatchev Avatar answered Sep 19 '22 11:09

Dimitre Novatchev


This can be achieved using the xpath abs function.

<xsl:value-of select="abs(DifferenceInDays)"/>
like image 33
Jory Avatar answered Sep 21 '22 11:09

Jory