[xslt] Identical separators in format-number() and decimal-format



Title: Message

I am trying to internationalise my application, which means that I need to be able to display numbers in a format relevant to a particular language/locale. I thought I could do this simply with the following:

 

(a) Within my stylesheet define a different set of decimal-format settings for each language, as in:

 

<xsl:decimal-format name='en' decimal-separator = '.' grouping-separator = ',' />

<xsl:decimal-format name='fr' decimal-separator = ',' grouping-separator = ' ' />

<xsl:decimal-format name='es' decimal-separator = ',' grouping-separator = '.' />

 

(b) Pass the 2-character language code to the transformation process in a parameter called $language.

 

(c) Format the output using the instruction:

 

<xsl:value-of select="format-number(number, '#,###,##0.00', $language)"/>

 

However, this is failing if the decimal-separator and grouping-separator in the pattern are different to those specified in decimal-format, and I am trying to understand why this should be an error. My understanding has always been that the pattern merely indicates where the separators go while decimal-format identifies which separators are to be used in the ouput. This understanding is based on the following:

 

(1) The number-to-be-formatted must always be supplied in the format 1234567.89 which means that:

            - there must be no grouping-separator

            - the decimal-separator must always be ‘.’ (period)

            - the negative sign (if present) must always be ‘-‘ (minus) and must always be the leftmost character

 

(2) The XSLT 1.0 specification for format_number() states that the format pattern string is in the syntax specified by the JDK 1.1 DecimalFormat class. This page is no longer available online, but a book that I have (Page 487, Java in a Nutshell, 3rd Edition, ISBN 1-56592-487-8, published by O’Reilly in 1999) defines the pattern as using ‘.’ (period) for the decimal-separator and ‘,’ (comma) for the grouping-separator. It does not say that these must be as you want them in the resulting output. This is repeated in the latest J2SE 1.4.2 DecimalFormat class which also states “The prefixes, suffixes, and various symbols used for infinity, digits, thousands separators, decimal separators, etc. may be set to arbitrary values, and they will appear properly during formatting.” I take this to mean that the separators in the decimal-format and the pattern need not be the same, and that it is the separators defined in decimal-format that will be used in the output, not the separators defined in the pattern.

 

(3) The XSLT 2.0 specification for format-number() gives a clearer set of error conditions, but it does NOT state that it is an error if the separators in the pattern are not identical to the separators in decimal-format.

 

(4) The idea that you must provide values for the separators in both the pattern and decimal-format, and that these values must be the same strikes me as being illogical. If you must define exactly the same values in two places, then surely one of the definitions is redundant. If you are forced to provide definitions in two different places it is only natural to assume that the two definitions may be different. In other words, the separators used in the pattern need not be the same as the separators used in the decimal-format. The separators in the pattern are simply there to identify WHERE they are to appear in the output string, whereas the separators in decimal-format define HOW they are to appear.

 

(5) I have downloaded a set of XML files from the Common Locale Data Repository (CLDR) Project which provides volumes of information on all the different locales. In particular it identifies what number separators (decimal and grouping) are used for each locale, but the pattern provided ALWAYS uses a decimal-separator of ‘.’ (period) and a grouping-separator of ‘,’ (comma).
 
Which interpretation is correct? Which interpretation is the most logical? Which interpretation is easiest to implement?
 
Tony Marston
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]