XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / decimal-format
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
xsl:decimal-format is a top-level element. The declaration provides XSLT function format-number() with a named collection of formatting characters and replacement strings for converting numbers to formatted output.
Classification | Declaration |
Function group | Output format definition |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:decimal-format may appear any number of times at the top level as a direct child of xsl:stylesheet. It is forbidden inside template bodies and is always empty.
Attributes:
Standard attributes apply to xsl:decimal-format. Several optional attributes also have predefined defaults.
decimal-separator
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The decimal-separator attribute defines the output decimal separator. The default is the Anglo-American full stop “.”.
digit
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The character in digit marks optional positions omitted when no digit greater than 0 is assigned. The default is “#”. After the decimal separator, trailing zero positions disappear. Excess fractional digits are omitted, rounding the last displayed digit.
grouping-separator
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The grouping-separator attribute defines the separator between digit groups, such as thousands. Its Anglo-American default is comma “,”.
Note: Groups need not contain three digits; their size is freely defined in the integer part of the pattern supplied to format-number().
infinity
Value | string |
Usage | Optional |
Introduction | XSLT 1.0 |
The infinity attribute defines the output string for infinity. The default is “infinity”.
minus-sign
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The minus-sign attribute defines the sign for negative numbers. The default is minus “-”.
name
Value | |
Usage | Optional |
Introduction | XSLT 1.0 |
The name attribute names a decimal-format declaration using a QName. Without the name attribute, the declaration replaces the default and applies when format-number() is called without a decimal-format name.
NaN
Value | string |
Usage | Optional |
Introduction | XSLT 1.0 |
The NaN attribute defines the replacement output for value NaN (Not a Number). Its default string is “NaN”.
pattern-separator
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The pattern-separator attribute defines the separator between positive and negative subpatterns. The default is semicolon “;”.
percent
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The percent attribute defines the percent symbol, default “%”. The number is multiplied by 100 before output.
per-mille
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The per-mille attribute defines the per-mille symbol, default “‰”, represented in the declaration as a Unicode character entity ‰. The number is multiplied by 1000 before output.
zero-digit
Value | char |
Usage | Optional |
Introduction | XSLT 1.0 |
The character in zero-digit marks positions filled with zeros when no digit greater than 0 is assigned. The default is “0”. This forces leading or trailing zeros in output.
Purpose:
The declaration xsl:decimal-format defines formatting characters and replacement output for overflow (±INF) and invalid operations (NaN). The required declaration is selected by name from function format-number().
The pattern controls the actual formatting.
The declaration only supplies characters and replacement strings, not the final layout or digit count. The formatting pattern is supplied as the second argument to format-number().
The function format-number():
Formally, the parameters of XSLT function format-number() are:
format-number($eingabezahl, $formatierungsmuster, $formatdefinition?)
The first argument, $eingabezahl, an XPath expression, determines the number. The required second argument $formatierungsmuster contains the output pattern. The optional third argument names a decimal format declared with xsl:decimal-format.
The call’s pattern uses characters from the named declaration. Without a third argument, default characters apply.
Examples:
Example 1 – Western European decimal output:
<xsl:decimal-format name="westeuropa" decimal-separator="," grouping-separator="."/>
Invoke it as follows:
format-number(sum(//wert),'#.###.##0,00','westeuropa')
Different input numbers and patterns with that declaration produce:
10000.23 with '#.###.##0,00' gives 1.000.023,00
400.2307 with '#.###.##0,00' gives 400,23
Example 2 – Applying a prefix:
With a prefix, a number can be formatted as a price:
400.2307 with 'Euro #.###.##0,00' gives Euro 400,23
Example 3 – Localisation:
Other attributes can also be localised.
<xsl:decimal-format name="de-westeuropa" decimal-separator="," grouping-separator="." infinity="Ueberlauf" NaN="verbotene Operation"/>
This fictitious call
format-number('hallo','#.###.###,000','de-westeuropa')
produces the declared NaN warning “verbotene Operation”, because it attempts to format a string.
format-number(1 div 0,'#.###.###,000','de-westeuropa')"
instead outputs, for value infinity from division by 0, the declared message “Ueberlauf”.
Element definition:
XSLT 1.0:
<!-- Category: top-level-element -->
<xsl:decimal-format
name = qname
decimal-separator = char
grouping-separator = char
infinity = string
minus-sign = char
NaN = string
percent = char
per-mille = char
zero-digit = char
digit = char
pattern-separator = char
/>
XSLT 2.0:
<!-- Category: declaration -->
<xsl:decimal-format
name? = qname
decimal-separator? = char
grouping-separator? = char
infinity? = string
minus-sign? = char
NaN? = string
percent? = char
per-mille? = char
zero-digit? = char
digit? = char
pattern-separator? = char
/>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn