Here is a single XPath expression implementing the abs()
function:
($x >= 0)*$x - not($x >= 0)*$x
This evaluates to abs($x)
.
Here is a brief demonstration of this in 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 transformation is applied to the following XML document:
<t>
<num>-3</num>
<num>0</num>
<num>5</num>
</t>
the wanted, correct result (abs() on every number) is produced:
<t>
<num>3</num>
<num>0</num>
<num>5</num>
</t>