[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: power

Subject: Re: power
From: Goetz Bock <bock@xxxxxxxxxxx>
Date: Tue, 22 May 2001 16:40:08 +0200
re power
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.