16

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?

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Mithil
  • 3,700
  • 12
  • 30
  • 41

4 Answers4

36

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.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
6

This can be achieved using the xpath abs function.

<xsl:value-of select="abs(DifferenceInDays)"/>
Jory
  • 155
  • 6
4

Some of the answers are complicating life way too much for XSLT 1.0 it's actually much simpler. Using the number formatting you can define a structure for Positive and Negative numbers, the default negative is -0 however you can define your own.

<xsl:value-of select='format-number(DifferenceInDays,"0;0")'/>

The above code will show the absolute value of Difference in Days just by using the provided formatting function.

Jon Mifsud
  • 163
  • 2
  • should work across as far as I'm aware. The format-number functionality is provided in the XSLT 1.0 spec. – Jon Mifsud Aug 22 '14 at 08:14
  • 1
    Have you tested this with **any** one of them? If yes, in which one did it work? – michael.hor257k Aug 22 '14 at 08:31
  • tested and used in XSLT 1.0 processed using PHP. I used the same trick to format negative numbers to show them in brackets rather then with a negative number as that is the correct format for financial institutions. – Jon Mifsud Aug 22 '14 at 08:42
  • 1
    You're not answering my question. PHP is not an XSLT processor. [Xalan-J](http://xsltransform.net/gWmuiHV), [Saxon 6.5](http://xsltransform.net/gWmuiHV/1) and libxslt are. This doesn't work in any one of the three. – michael.hor257k Aug 22 '14 at 08:50
  • it certainly works in LibXSLT (only processor that supports PHP) - just tried it now. And you can find information on how the format number works in W3 Schools, it's quite simple http://www.w3schools.com/xsl/func_formatnumber.asp – Jon Mifsud Aug 22 '14 at 08:57
  • I *know* how format-number() works. And my information does not come from W3 Schools. However, it doesn't work on **my** libxslt - and if you'll click the links above, you'll see that it doesn't work in the other two processors either. Whether that's a correct interpretation of the XSLT 1.0 spec (which is actually the JDK 1.1 spec) is open to debate - but the fact remains that all three vendors thought so. – michael.hor257k Aug 22 '14 at 09:23
  • seems strange to be inconsistent within libxslt itself. I'll see weather there's another way to test this. I think it's very unlikely to be tied to something else other then the libxslt processor. – Jon Mifsud Aug 22 '14 at 09:51
  • Also just for reference Saxon 9 from the links you provided - the example I gave also works. So most likely it was a misinterpretation of the spec compared to previous versions. – Jon Mifsud Aug 22 '14 at 09:52
  • 5
    Yes, it does work in Saxon 9 - which is an **XSLT 2.0** processor, thus follows a different specification. In any case, if you want a simple solution using string manipulation that works universally, try `translate($number, '-', '')`. – michael.hor257k Aug 22 '14 at 10:11
1

diffInDays * (1 - 2*(diffInDays &lt; 0))