Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Changes in 2.0
In XSLT 1.0, the effect of the function was defined by reference to the Java JDK 1.1 specifications. This created problems because the JDK 1.1 description left many details underspecified, and later versions of the JDK not only clarified the specification, but also added new features. It was therefore decided in XSLT 2.0 to provide a freestanding definition of this function. This is largely compatible with the old JDK 1.1 specification, but the rules are now much more precise, and in corner cases they will not necessarily give the same results as implementations based on the JDK, let alone non-Java implementations. For example, the JDK 1.1 specification did not say how rounding was done. The actual JDK 1.1 implementation used the rule of rounding a final 5 to the nearest even number, and this is the rule that XSLT 2.0 has adopted, but XSLT 1.0 processors might well do rounding differently.
Signature
Argument | Type | Meaning |
value | Numeric? | The number to be formatted |
picture | xs:string | A picture string identifying the way in which the number is to be formatted |
format (optional) | xs:string | A string in the form of a lexical QName, that identifies an |
Result | xs:string? | The formatted number |
Effect
The function returns a string value; this is the result of formatting the given
value
using the format picture supplied in
picture
, while applying the rules defined in the decimal format named in
format
if present, or using the default decimal format otherwise.
The decimal-format Name
The
format
argument, if it is present, must take the form of a lexical
QName
; that is, an XML name optionally prefixed with a namespace prefix that corresponds to a namespace declaration that is in scope at the point in the stylesheet where the
format-number()
function is called. There must be an
If the
format
argument is omitted, the default decimal format is used. A default decimal format can be established for a stylesheet by including an
The Picture String
The structure of the
picture
string is as follows. Here, and in the text that follows, I will use the default characters for each role: for example
;
as the pattern separator,
.
as the decimal point,
0
as the zero digit,
#
as the optional digit placemarker, and so on. Remember, though, that you can change the characters that are used in each of these roles using the
Construct | Content |
picture | subpicture ( ; subpicture)? |
subpicture | prefix? integer ( . fraction)? suffix? |
prefix | Any characters except special characters |
suffix | Any characters except special characters |
integer | # * 0 * (but also allowing , to appear) |
fraction | 0 * # * (but also allowing , to appear) |