number() Function  
Converts its argument to a number.
 
Inputs

An object. The object is converted to a number as described in the following subsection.

 
Output

A number. The object is converted to a number as follows:

    If the argument is a boolean value, the value true is converted to the number 1; the value false is converted to the number 0.

    If the argument is a node-set, the node-set is converted to a string as if it were passed to the string() function, then that string is converted to a number like any other string. (Remember that the string() function returns the string value of the first node in the node-set.)

    If the argument is a string, it is converted as follows:

      If the string consists of optional whitespace, followed by an optional minus sign (-), followed by a number, followed by whitespace, it is converted to the floating-point value nearest to the mathematical value represented by the string. (The IEEE 754 standard defines a round-to-nearest rule; see the standard for more information.)

      Any other string is converted to the value NaN (not a number).

    If the argument is any other type, it is converted to a number in a way that depends on that type. See the documentation for your XSLT processor to find out what other types are supported and how they are converted to numbers.

 
Defined in

XPath section 4.4, Number Functions.

 
Example

Here is the XML document we'll use to test the number() function:

<?xml version="1.0"?>
<report>
  <title>Miles Flown in 2001</title>
  <month sequence="01">
    <miles-flown>12379</miles-flown>
    <miles-earned>35215</miles-earned>
  </month>
  <month sequence="02">
    <miles-flown>32857</miles-flown>
    <miles-earned>92731</miles-earned>
  </month>
  <month sequence="03">
    <miles-flown>19920</miles-flown>
    <miles-earned>76725</miles-earned>
  </month>
  <month sequence="04">
    <miles-flown>18903</miles-flown>
    <miles-earned>31781</miles-earned>
  </month>
</report>

We'll test the number() function with a variety of arguments:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>


  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Tests of the number() function:</xsl:text>

    <xsl:value-of select="$newline"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   number(true())=</xsl:text>
    <xsl:value-of select="number(true())"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   number(false())=</xsl:text>
    <xsl:value-of select="number(false())"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   number(/report/month[2]/miles-flown)=</xsl:text>
    <xsl:value-of select="number(/report/month[2]/miles-flown)"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   number(//miles-flown)=</xsl:text>
    <xsl:value-of select="number(//miles-flown)"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   number(/report/title)=</xsl:text>
    <xsl:value-of select="number(/report/title)"/>
  </xsl:template>

</xsl:stylesheet>

The output of our stylesheet looks like this:


Tests of the number() function:

   number(true())=1
   number(false())=0
   number(/report/month[2]/miles-flown)=32857
   number(//miles-flown)=12379
   number(/report/title)=NaN