<xsl:decimal-format> |
Defines a number format to be used when writing numeric values to the output document. If the <decimal-format> does not have a name, it is assumed to be the default number format used for all output. On the other hand, if a number format is named, it can be referenced from the format-number() function.
Category
Top-level element
Required Attributes
None.
Optional Attributes
name Gives a name to this format. decimal-separator Defines the character (usually either a period or comma) used as the decimal point. This character is used both in the format string and in the output. The default value is the period character ( . ). grouping-separator Defines the character (usually either a period or comma) used as the thousands separator. This character is used both in the format string and in the output. The default value is the comma ( , ). infinity Defines the string used to represent infinity. Be aware that XSLT's number facilities support both positive and negative infinity. This string is used only in the output. The default value is the string " Infinity " . minus-sign Defines the character used as the minus sign. This character is used only in the output. The default value is the hyphen character ( - , #x2D). NaN Defines the string displayed when the value to be formatted is not a number. This string is used only in the output; the default value is the string " NaN " . percent Defines the character used as the percent sign. This character is used both in the format string and in the output. The default value is the percent sign ( % ). per-mille Defines the character used as the per-mille sign. This character is used both in the format string and in the output. The default value is the Unicode per-mille character (#x2030). zero-digit Defines the character used for the digit zero. This character is used both in the format string and in the output. The default is the digit zero ( 0 ). digit Defines the character used in the format string to stand for a digit. The default is the number sign character ( # ). pattern-separator Defines the character used to separate the positive and negative subpatterns in a pattern. The default value is the semicolon ( ; ). This character is used only in the format string.
Content
None. <xsl:decimal-format> is an empty element.
Appears in
<xsl:decimal-format> is a top-level element and can only appear as a child of <xsl:stylesheet>.
Defined in
XSLT section 12.3, Number Formatting.
Example
Here is a stylesheet that defines two <decimal-format>s:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:months="Lookup table for month names">

      <xsl:output method="text"/>

      <months:name sequence="01">January</months:name>
      <months:name sequence="02">February</months:name>
      <months:name sequence="03">March</months:name>
      <months:name sequence="04">April</months:name>

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

      <xsl:decimal-format name="f1"
        decimal-separator=":"
        grouping-separator="/"/>

      <xsl:decimal-format name="f2"
        infinity="Really, really big"
        NaN="[not a number]"/>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:text>Tests of the <decimal-format> element:</xsl:text>

        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(1528.3, '#/###:00', 'f1')=</xsl:text>
        <xsl:value-of select="format-number(1528.3, '#/###:00;-#/###:00', 'f1')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(1 div 0, '###,###.00', 'f2')=</xsl:text>
        <xsl:value-of select="format-number(1 div 0, '###,###.00', 'f2')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(blue div orange, '#.##', 'f2')=</xsl:text>
        <xsl:value-of select="format-number(blue div orange, '#.##', 'f2')"/>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="report/month">
          <xsl:text>   </xsl:text>
          <xsl:value-of
            select="document('')/*/months:name[@sequence=current()/@sequence]"/>
          <xsl:text> - </xsl:text>
          <xsl:value-of select="format-number(miles-flown, '##,###')"/>
          <xsl:text> miles flown, </xsl:text>
          <xsl:value-of select="format-number(miles-earned, '##,###')"/>
          <xsl:text> miles earned.</xsl:text>
          <xsl:value-of select="$newline"/>
          <xsl:text>     (</xsl:text>
          <xsl:value-of
            select="format-number(miles-flown div sum(//miles-flown), '##%')"/>
          <xsl:text> of all miles flown, </xsl:text>
          <xsl:value-of
            select="format-number(miles-earned div sum(//miles-earned), '##%')"/>
          <xsl:text> of all miles earned.)</xsl:text>
          <xsl:value-of select="$newline"/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
        <xsl:text>   Total miles flown: </xsl:text>
        <xsl:value-of select="format-number(sum(//miles-flown), '##,###')"/>
        <xsl:text>, total miles earned: </xsl:text>
        <xsl:value-of select="format-number(sum(//miles-earned), '##,###')"/>
      </xsl:template>

    </xsl:stylesheet>
We'll use this stylesheet against the following document:
    <?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>
When we process this document with the stylesheet, here are the results:
    Tests of the <decimal-format> element:

       format-number(1528.3, '#/###:00', 'f1')=1/528:30
       format-number(1 div 0, '###,###.00', 'f2')=Really, really big
       format-number(blue div orange, '#.##', 'f2')=[not a number]

       January - 12,379 miles flown, 35,215 miles earned.
         (15% of all miles flown, 15% of all miles earned.)

       February - 32,857 miles flown, 92,731 miles earned.
         (39% of all miles flown, 39% of all miles earned.)

       March - 19,920 miles flown, 76,725 miles earned.
         (24% of all miles flown, 32% of all miles earned.)

       April - 18,903 miles flown, 31,781 miles earned.
         (22% of all miles flown, 13% of all miles earned.)

       Total miles flown: 84,059, total miles earned: 236,452