[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] format-number in XSLT 1.0

Subject: RE: [xsl] format-number in XSLT 1.0
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 25 Jan 2005 17:55:31 -0000

The Xalan result is conformant with the XSLT 1.0 specification if you adopt
a generous interpretation of the spec; the Saxon 8.2 result is conformant
with the XSLT 2.0 specification.

In XSLT 1.0 format-number() was specified by reference to the JDK 1.1
specification of the DecimalFormat class. Unfortunately the JDK 1.1
documentation was very vague in a number of areas. The statement that the
negative subpicture is largely ignored appeared in the JDK documentation
only in later versions. It's very likely that an XSLT 1.0 implementation
built using Java will behave the way DecimalFormat actually behaves in the
version of Java that you are using, while an implementation built in a
different language is likely to do what a reader of the JDK 1.1
specification might have expected, namely that the negative subpicture means
what it says. 

In XSLT 2.0 we decided to remove the dependency on the Java specification
and write a free-standing specification for format-number. In doing this, we
had to decide whether to make it behave the way Java behaved, or the way JDK
1.1 said it should behave; and by-and-large we chose the latter, because
that is what non-Java XSLT implementations are likely to have implemented.

So XSLT 2.0 doesn't carry forward this Java anomaly regarding negative
patterns. This is documented as something that may cause incompatible
behavior on some implementations, though since XSLT 1.0 is underspecified in
this area that's outside the control of the spec.

Saxon 8.2 has a free-standing implementation of format-number that doesn't
rely on the Java DecimalFormat class, so it should do exactly what XSLT 2.0

If you need it, the old implementation remains available in Saxon 8.2 for
the time being under the name format-number-1.0().

Michael Kay

> -----Original Message-----
> From: Jack Matheson [mailto:jack@xxxxxxxxxxxxxx] 
> Sent: 25 January 2005 17:14
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] format-number in XSLT 1.0
> I created a small example stylesheet to see how positive and negative 
> format patterns are used, but something doesn't make sense to me.
> Given the following stylesheet:
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>   <xsl:output method="text"/>
>   <xsl:template match="/">
>      pos: <xsl:value-of select="format-number(31415, 
> '##,##.0;0000')"/>
>      neg: <xsl:value-of select="format-number(-31415, 
> '##,##.0;0000')"/>
>   </xsl:template>
> </xsl:stylesheet>
> Xalan (bundled with my Java distribution) produces:
>      pos: 3,14,15.0
>      neg: -3,14,15.0
> Saxon (8.2) produces
>      pos: 3,14,15.0
>      neg: 31415
> Can anyone tell me which is correct? In Kay's XSLT 1.0 reference, I 
> found this:
> "If there is an explicit negative subpattern, it serves only 
> to specify 
> the negative prefix and suffix; the number of digits, minimal digits, 
> and other characteristics are all the same as the positive pattern."
> Doesn't this mean that the negative pattern should be 
> identical to the 
> positive pattern with a "-" prefix?
> Any clarification is greatly appreciated =)

Current Thread