Name

[2.0] abs()

Returns the absolute value of a numeric argument.

Syntax

numeric? abs(numeric?)

Input

A numeric value.

Output

The absolute value of the given numeric value.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 6.4, “Functions on Numeric Values.”

Example

Here’s a short stylesheet that tests the abs() function:

<?xml version="1.0"?>
<!-- abs.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>&#xA;Here are some tests of the abs() function:&#xA;</xsl:text>
    <xsl:text>&#xA;  abs(7) = </xsl:text>
    <xsl:value-of select="abs(7)"/>
    <xsl:text>&#xA;  abs(-7) = </xsl:text>
    <xsl:value-of select="abs(-7)"/>
    <xsl:text>&#xA;  abs(0) = </xsl:text>
    <xsl:value-of select="abs(0)"/>
    <xsl:text>&#xA;  abs(-0) = </xsl:text>
    <xsl:value-of select="abs(-0)"/>

    <!-- An XSLT 2.0 processor won’t run this example at all. -->
    <!-- <xsl:value-of select="abs('x')"/> -->

    <xsl:variable name="testSequence" as="xs:integer*" select="1 to 10"/>
    <xsl:text>&#xA;  $testSequence = </xsl:text>
    <xsl:value-of select="$testSequence" separator=", "/>
    <xsl:text>&#xA;  abs(count($testSequence)) = </xsl:text>
    <xsl:value-of select="abs(count($testSequence))"/>
  </xsl:template>

</xsl:stylesheet>

Here are the results:

Here are some tests of the abs() function: abs(7) = 7 abs(-7) = 7 abs(0) = 0 abs(-0) = 0 $testSequence = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 abs(count($testSequence)) ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.