BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


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


Uncategorized
Re: Missing constant definition
A simple template to display singular and plural counts
swapping two integer values without using temporary
Debugging script
Combinations of k out of n numbers
Permutations of the values of N nodes
What is BEEP (BXXP)?
Family Tree maker
Alternative Ordering - ""Women and Children to the lifeboats first""
Meta-Patterns: Design Patterns Explained
XML Helper functions in Visual Basic
nodeset of child templates using document()
Converting INI files to XML
Table with more than one column
Create an N-Column Table from XML node-set
Fullxml 2
Randomization of node-sets or node-lists
Sorting the results and use the Axis
Finding the Maximum Date
TreeMaker


 
 

<< System.XMLWCF, WS, SOAP >>


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

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 :  

<< System.XMLWCF, WS, SOAP >>





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

 
 

    Email TopXML