views:

229

answers:

2

There is a task to output some data taken from database using XSLT. The application current locale is ru_RU, so decimal separator is comma. It looks like

<data>
  <row>
    <date value="28.04.2010"/>
    <clicks value="281"/>
    <depth value="1,7"/>
  <row>
  <row>
    <date value="29.04.2010"/>
    <clicks value="15"/>
    <depth value="3"/>
  <row>
<data>

It’s needed to format "depth" value as decimal number: 1,70 and 3,00. When I try to transform the value ising:

<xsl:value-of select="format-number(number(depth/@value)), '##.##'"/>

I get NaN because XSLT-processor doesn’t consider comma as a decimal separator when casting attribute value to number.

It helps somehow when I manually "translate" the source string from "decimal comma" format to "decimal point" one

<xsl:value-of select="format-number(number(translate(depth/@value, ',', '.'))), '##.##'"/>

but it looks rather ugly.

Is there any more correct and centralized way to tell the processor that comma sign is a decimal separator?

+1  A: 

Have you tried using the locale parameter of format-number? (format-number has a third optional parameter)

baol
don't mind, it does not work.
baol
Also, if your locale is correctly set and your parser supports the localization you might need to get rid of the "number" call, AFAI understand it's not localized.
baol
The processor is XSLTProcessor in PHP 5.3, I haven’t found any mentions about its localization.The application locale is set correctly: <?php setlocale(LC_ALL, 'ru_RU.koi8r'); echo 0.26; >> 0,26
Sergei Morozov
The format-number function accepts an argument of "number" type and casts it to string using the provided formatting options. So, before formating the node value it’s needed to cast it to number. And that’s the problem.
Sergei Morozov
+2  A: 

There is no more correct or centralized way. The way I do this is to pre-process the XML file with a special xslt which transforms the numbers the way you do it too.

Jan Willem B