Re: [xsl] simple question

Subject: Re: [xsl] simple question
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 21 Feb 2002 09:34:39 +0000
Hi Arun,

> How do i incorporate data formatting, while writing an XSL?

One way to do it is to create named templates that handle the values
that you want to format, and format them in whatever way you define.
For example, you could have a template that formats numbers, accepting
a number as the argument (here it defaults to the numerical value of
the current node at the point where you call the template - that makes
calling it easier later on):

<xsl:template name="format-number">
  <xsl:param name="number" select="number()" />
  ...
</xsl:template>

And a template that formats prices by adding a dollar sign before the
formatted number:

<xsl:template name="format-price">
  <xsl:param name="price" select="number()" />
  <xsl:text>$</xsl:text>
  <xsl:call-template name="format-number">
    <xsl:with-param name="number" select="$price" />
  </xsl:call-template>
</xsl:template>

> for example,
>
> if i have <market-cap>1000000000</market-cap>
>
> I need to format this to $1 billion after i apply the transformation

Here you have a market-cap element whose value you want to format as a
price. You could have a template matching the market-cap element,
which called the format-price element (no need to pass the parameter -
it will default to the numberical value of the market-cap element):

<xsl:template match="market-cap">
  <xsl:call-template name="format-price" />
</xsl:template>

Of course how you write the formatting code is really up to you. If
that was the part that you wanted help with, then you should supply
some more examples, or preferably the general rules that determine how
a number should be formatted. How do you want the number 123456789 to
be formatted, for example?

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread