XSLT
XSLT Contents
Summary This is an XSLT implementation of a function that calculates a^b (a to the power of b) for any positive integer base a and any integer power b.
The source code is self-explanatory. This is a typical recursive template. Do notice how the factor and increment are established according to whether pow is non-negative or negative.
Also, be aware that deep recursive processing will crash your MSXML -- I succeeded with base=1 and pow=10000 on a 128MB RAM P-266 computer. In this case MSXML crashed in about 30 seconds.
On the same computer with my ol' Saxon 4.6 the correct value was calculated for about 5 seconds.
Here's a sample xml source and the stylesheet -- when applied the result is:
0.008
Note also, that in a production environment system there should be some validation added to ensure that a and b have correct (permissible) values.
Source xml:-------------
Stylesheet:------------<xsl:stylesheet version=1.0> <xsl:output method=text/> <xsl:template match=/> <xsl:call-template name=pow> <xsl:with-param name=base select=/power/base/> <xsl:with-param name=pow select=/power/pow/> </xsl:call-template> </xsl:template> <xsl:template name=pow> <xsl:param name=base select=1/> <xsl:param name=pow select=0/> <xsl:param name=tmpResult select=1/> <xsl:variable name=result> <xsl:choose> <xsl:when test=$pow >= 0> <xsl:value-of select=$tmpResult * $base/> </xsl:when> <xsl:otherwise> <xsl:value-of select=$tmpResult div $base/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name=incr> <xsl:choose> <xsl:when test=$pow >= 0> <xsl:value-of select=- 1/> </xsl:when> <xsl:otherwise> <xsl:value-of select=1/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test=$pow = 0> <xsl:value-of select=$tmpResult/> </xsl:when> <xsl:otherwise> <xsl:call-template name=pow> <xsl:with-param name=base select=$base/> <xsl:with-param name=pow select=$pow + $incr/> <xsl:with-param name=tmpResult select=$result/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template></xsl:stylesheet>
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting