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

為{幸葍}努か 提交于 2019-11-27 17:43:18

问题


I have

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

DifferenceInDays can be negative or positive, I want to display it as positive. How can I do this?


回答1:


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.




回答2:


This can be achieved using the xpath abs function.

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



回答3:


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




回答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.



来源:https://stackoverflow.com/questions/804421/how-can-i-calculate-the-absolute-value-of-a-number-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!