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

Re: [xsl] Formatting of numbers, dynamically


Subject: Re: [xsl] Formatting of numbers, dynamically
From: "Joerg Heinicke" <joerg.heinicke@xxxxxx>
Date: Mon, 11 Mar 2002 23:21:26 +0100

Here we need again recursive template: counting down the @minor_unit, for
every processing of the template 'minor_unit' one '0'.

the XML

<root>
    <number minor_unit="3">1.123</number>
    <number minor_unit="2">1.000</number>
    <number minor_unit="4">0.120</number>
    <number minor_unit="3">12</number>
</root>

the XSL:
<xsl:template match="number">
    <xsl:variable name="format-string">
        <xsl:text>#,##0.</xsl:text>
        <xsl:call-template name="minor_unit">
            <xsl:with-param name="minor_unit" select="@minor_unit"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$format-string"/><br/>
    <xsl:value-of select="format-number(., $format-string)"/><br/>
</xsl:template>

<xsl:template name="minor_unit">
    <xsl:param name="minor_unit" select="2"/>
    <xsl:text>0</xsl:text>
    <xsl:if test="$minor_unit > 1">
        <xsl:call-template name="minor_unit">
            <xsl:with-param name="minor_unit" select="$minor_unit - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

the output:
#,##0.000
1.123
#,##0.00
1.00
#,##0.0000
0.1200
#,##0.000
12.000

Regards,

Joerg

> I am current using format-number($node, '#,##0.00') for formatting
currency.
> We are now implementing foreign currencies into our application.  For
every
> transaction, I have an element, CurrencyCode, which contains the ISO
> currencyCode and an attribute @minor_unit which tells me how many digits
> follow the decimal seperator.  My question is how I can dynamically build
> the formatting string.
>
> I was reading this morning in the March 2002 issue of MSDN magazine about
> how XSLT is a "Functional programming language", and therefore
xsl:variable
> entities are "not really variables, rather runtime-assigned constants"
> because once they are assigned, the values cannot be changed.  So, this
lead
> me to start thinking about how I could wrap an xsl:for-each with the
> xsl:variable element to produce my output.  What I don't know if how to
> obtain functionality like:
>
> for(i=0;i<=@minor_unit;i++){ ... };
>
> I do not want to use an enbedded JScript solution because I want the XSL
to
> be portable (namely between MSXML and Xalan).  So, how would I construct
the
> @select attribute in the xsl:for-each to just loop a predetermined number
of
> times?
>
> --
>
> David B. Bitton
> david@xxxxxxxxxxxxxx
> www.codenoevil.com
>
> Diversa ab illis virtute valemus.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords