Re: [xsl] power

Subject: Re: [xsl] power
From: Goetz Bock <bock@xxxxxxxxxxx>
Date: Tue, 22 May 2001 16:40:08 +0200
On Tue, May 22 '01 at 16:48, SoftLiban ITANI Mohamed wrote:
> I need to do some calculations in my xsl, is there any power function??
> 2 power 3 = 8
No, but you can write a recursive template:

<xsl:template name="power">
  <xsl:param name="value">1<xsl:param>
  <xsl:param name="base">1<xsl:param>
  <xsl:param name="power">0<xsl:param>

  <xsl:choose>
    <xsl:when test="$power=0">
      <xsl:value-of select="$value"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="power">
        <xsl:with-param name="value">
          <xsl:value-of select="$value * $base"/>
        </xsl:with-param>  
        <xsl:with-param name="base">
          <xsl:value-of select="$base"/>
        </xsl:with-param>
        <xsl:with-param name="power">
          <xsl:value-of select="$power - 1"/>
        </xsl:with-param>
      </xsl:call-template>  
    </xsl:otherwise>  
  </xsl:choose>  
<xsl:template>

(NOTE: this was all done with out any xslt documentation and I did not
       check it, so look out for typos)

you use it like this $result = $base power $power:

<xsl:variable name="result">
  <xsl:call-template name="power">
    <xsl:with-param name="base">
      <xsl:value-of select="$base"/>
    </xsl:with-param>
    <xsl:with-param name="power">
      <xsl:value-of select="$power"/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:variable>

Cu,
    Goetz.

Attachment: pgp00006.pgp
Description: PGP signature

Current Thread