BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
Grouping an XML Node by First Letter
Dependent ComboBox in HTML build with XSLT
Re: How to use XSL function sum() to sum 2 XML fields
Re: How to use XSL function sum() to sum 2 XML fields
How to use XSL function sum() to sum 2 XML fields
A simple template to display singular and plural counts
Helpful XSLT ""break"" template
Support automatic transformation using XSLT in IE in side by side mode
Displaying XML data in Pages
Repeated search and replace in XSLT
Combinations of k out of n numbers
Permutations of the values of N nodes
DVC Algorithms in XSLT
Another way of avoiding XSLT processor crash
Re: DVC Algorithms in XSLT
trig functions in xslt
XSLT string Replace function
A Slide-In Menu made with XML/XSLT
What is SVG?
Family Tree maker


 
 

<< XQuery.NET and XML >>


By Dimitre Novatchev
First Posted 07/06/2001
Times viewed 479

The pow(a, b) function


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:
-------------

<power>
    <base>
        5
    </base>
    <pow>
        -3
    </pow>
</power>

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>

Additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML