XSLT and currency

Just some simple code this time, a minor XSLT function to
change values to a nice Currency Value. Like it says, it formats a number (some
var names are Dutch, sorry for that):

<xsl:templatename="FormatNumber">

      <xsl:paramname="str"/>

      <xsl:paramname="defnadekomma"/>

      <xsl:variablename="newvalueS">

            <xsl:value-ofselect="string($str)"/>

      </xsl:variable>

      <xsl:variablename="newvalue">

            <xsl:choose>

                  <xsl:whentest="contains($newvalueS,
',')"
>

                        <xsl:value-ofselect="substring-before($newvalueS,
',')"
/>

                  </xsl:when>

                  <xsl:otherwise>

                        <xsl:value-ofselect="$newvalueS"/>

                  </xsl:otherwise>

            </xsl:choose>

      </xsl:variable>

      <xsl:variablename="nadekomma">

            <xsl:choose>

                  <xsl:whentest="contains($newvalueS,
',')"
>

                        <xsl:value-ofselect="concat(',',substring-after($newvalueS,
','))"
/>

                  </xsl:when>

            </xsl:choose>

      </xsl:variable>

      <xsl:variablename="nadekommaMax">

            <xsl:choose>

                  <xsl:whentest="string-length($nadekomma)
&gt; 3"
>

                        <xsl:value-ofselect="substring($nadekomma,
1, 3)"
/>

                  </xsl:when>

                  <xsl:otherwise>

                        <xsl:value-ofselect="$defnadekomma"/>

                  </xsl:otherwise>

            </xsl:choose>

      </xsl:variable>

      <xsl:choose>

            <xsl:whentest="string-length($newvalue)
&gt; 3"
>

                  <xsl:variablename="newvaluews">

                        <xsl:value-ofselect="substring($newvalue,1,string-length($newvalue)
– 3)"
/>.<xsl:value-ofselect="substring($newvalue,string-length($newvalue)
– 2,3)"
/>

                  </xsl:variable>

                  <xsl:value-ofselect="concat($newvaluews,
$nadekommaMax)"
/>

            </xsl:when>

            <xsl:otherwise>

                  <xsl:value-ofselect="concat($newvalue,
$nadekommaMax)"
/>

            </xsl:otherwise>

      </xsl:choose>

</xsl:template>

By using the following statement you get a nice Euro sign, and a price, changing the
€ to a $ or whatever value you want it to be, you can even change the currency.

<xsl:text></xsl:text>

<xsl:call-templatename="FormatNumber">

      <xsl:with-paramname="str"select="$prijs"/>

      <xsl:with-paramname="defnadekomma"select="',-'"/>

</xsl:call-template>

Leave a Reply