Here is one XPath expression that implements the abs() function:
($x >= 0)*$x - not($x >= 0)*$x
This evaluates to abs($x) .
Here is a brief demo of this action :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:param name="x" select="."/> <xsl:value-of select= "($x >= 0)*$x - not($x >= 0)*$x"/> </xsl:template> </xsl:stylesheet>
when this conversion is applied to the following XML document:
<t> <num>-3</num> <num>0</num> <num>5</num> </t>
the desired, correct result (abs () for each number) is created :
<t> <num>3</num> <num>0</num> <num>5</num> </t>
source share