Re: [xsl] Re: Format Number and Empy Elements

Subject: Re: [xsl] Re: Format Number and Empy Elements
From: "Abel Braaksma (online)" <abel.online@xxxxxxxxx>
Date: Wed, 8 Aug 2007 21:49:32 +0200 (CEST)
> Hi Guys
>
> I still have a few problems with this. Here's my sample XML
>
>
> RESULTS
>
> concat('0', value) works brilliantly, except when there are negative
> numbers (as I get '0-100'). There's always a catch

make it concat('0 ', value) and it will work.

>
> Strangely, sum(value[.]) still gave NaN for Element A
>

Yes, I was not correct. If a node does not exist, sum(value[.]) will not raise
an error, it will return zero. If a node exists, but has no value, you can do
sum(value[number(.)]). That also works if the node has non-numeric values
(meaning: it will give a sum of zero if the values are non-numeric).

> 	<xsl:for-each select="number">

Just out of curiosity, but why do you use for-each here? Using apply-templates
would work as well and is more adaptive to changes.

> <xsl:value-of
>     select="format-number(number(concat('0', value)), '#,.00')"/></td>

It's a matter of design of course, but if you want this less nested and
(again) more adaptive to change, you can also code it like:

<td>
   <xsl:apply-templates select="number" />
</td>

and:
<xsl:template match="number">
   <xsl:value-of select="..." />
</xsl:template>

or, if you prefer (and that removes the whole issue of NaN):
<xsl:template match="number" priority="0">0.00</xsl:template>
<xsl:template match="number[number(.)]">
    <xsl:value-of select="format-number(., '#,.00')" />
</xsl:template>



but to design it like that is of course largely a matter of taste ;)

Cheers,
-- Abel Braaksma

PS: not tested ;)

Current Thread