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

Re: [xsl] NaN and Infinity in format-number


Subject: Re: [xsl] NaN and Infinity in format-number
From: Jack Matheson <jack@xxxxxxxxxxxxxx>
Date: Fri, 13 Jan 2006 11:59:42 -0600

Thanks again for the great explanation!

-Jack

In the following stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
   <xsl:template match="/">
     1: <xsl:value-of select="format-number(0.0 div 0.0, '(#)')"/>
     2: <xsl:value-of select="format-number(1.0 div 0.0, '(#)')"/>
   </xsl:template>
</xsl:stylesheet>

...I see interesting results in Saxon 6 and Xalan 2.6:

     1: NaN
     2: (Infinity)

I was expecting either parenthesis around both values or no
parenthesis at all. Is this a result of the XSLT 1.0
processors using
DecimalFormat in the JDK?

Yes, almost certainly.


Is this the conforming XSLT 1.0 behavior?

XSLT 1.0 defines the spec in terms of the JDK 1.1.8 specification of Java
DecimalFormat. This is a pretty shoddy spec, as it happens. It says:


* NaN is formatted as a single character, typically \\uFFFD.

* +/-Infinity is formatted as a single character, typically \ \u221E, plus
the positive and negative pre/suffixes.


The "single character" is nonsense (though it's still there in the JDK 1.5
spec): DecimalFormatSymbols allows a String for these symbols! But in XSLT,
it's clearly a string.


But yes (to my surprise), it does seem to be an explicit part of the spec
that the prefix/suffix are included for Infinity, but not for NaN. My
surprise is because there are so many other aspects of the behavior that
aren't defined in any detail in the 1.1.8 spec, although in some cases they
have been defined more carefully in subsequent versions.


There's one notorious oddity: the JDK 1.1.8 implementation ignores most of
the negative subpattern, except for the prefix and suffix, but there is
nothing in the spec to suggest this. In subsequent versions of the JDK, this
behavior was documented.



I noticed Saxon 8 puts parenthesis around both values. Is this an an XSLT 2.0 action?


Yes. The XSLT 2.0 spec has cut loose from the original dependency on the JDK
documentation, and now contains a self-contained definition of the function.
See http://www.w3.org/TR/xslt20/#formatting-the-number. Rule 1 says that if
the value is NaN, it is output using the prefix and suffix from the positive
subpicture.


I hadn't actually noticed that this was an incompatibility from previous
behavior. I'll raise it as a comment on the spec for the WG to review.


Michael Kay
http://www.saxonica.com/


Current Thread
Keywords