Re: [xsl] calculating pow of numbers and a final sum

Subject: Re: [xsl] calculating pow of numbers and a final sum
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 22 Jan 2003 03:24:09 -0800 (PST)
--- "Andreas Mecky" <andreasmecky@xxxxxxxx> wrote:
> Hello everybody,
> 
> I am kind of a newbie to this mailing list and I have a problem that
> keeps
> me busy for some time now.
> I am working on a code generator for some java classes.
> This is my XML:
> 
> <?xml version="1.0" ?>
> <logging>
>   <target name="init"/>
>   <target name="debug"/>
>   <target name="fatal"/>
> </logging>
> 
> this should result in:
> 
> public static final int INIT = 1;
> public static final int DEBUG = 2;
> public static final int FATAL = 4;
> public static final int ALL = 8;
> 
> I can get everything working except the numbers. How can I calculate
> the
> numbers which
> are something like counter=2^(pos-1)?
> This all entry is generated automatically. At this point I need
> either
> to
> sum all former values
> or do the same calculation as mentioned above.
> 
> Some research on google did not helped me out.

Using FXSL one will utilise the powerof the "scanIter" template in the
following way:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:myDouble="f:myDouble"
 xmlns:vendor="urn:schemas-microsoft-com:xslt"
 exclude-result-prefixes="vendor myDouble">
 
 <xsl:import href="iter.xsl"/>
  
  <xsl:output method="text"/>
  
  <xsl:template match="/">
  
    <xsl:variable name="vrtfPartials">
      <xsl:call-template name="powers2">
        <xsl:with-param name="pBits" select="count(*/target)"/>
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:variable name="vPartials" 
     select="vendor:node-set($vrtfPartials)/*"/>
    
    <xsl:for-each select="*/target">
      <xsl:variable name="vPos" select="position()"/>
      
      <xsl:value-of select="concat('&#xA;public static final int ',
                                   @name,
                                   ' = ',
                                   $vPartials[$vPos]
                                   )"/>
    </xsl:for-each>
    
    <xsl:value-of select="concat('&#xA;public static final int ',
                                 'ALL',
                                 ' = ',
                                 2 * $vPartials[last()]
                                 )"/>
                                 
    Total is : <xsl:value-of 
    select="sum($vPartials) + 2 * $vPartials[last()]"/>
    
  </xsl:template>
  
  <xsl:template name="powers2">
    <xsl:param name="pBits"/>
    
    <xsl:variable name="vFunDouble" 
     select="document('')/*/myDouble:*[1]"/>
     
     <xsl:call-template name="scanIter">
       <xsl:with-param name="arg1" select="$pBits - 1"/>
       <xsl:with-param name="arg2" select="$vFunDouble"/>
       <xsl:with-param name="arg3" select="1"/>
     </xsl:call-template>
  </xsl:template>
  
  <myDouble:myDouble/>
  
  <xsl:template match="myDouble:*">
    <xsl:param name="arg1" select="0"/>
    
    <xsl:value-of select="2 * $arg1"/>
  </xsl:template>
  
</xsl:stylesheet>

When this transformation is applied against your source.xml:

<logging>
  <target name="init"/>
  <target name="debug"/>
  <target name="fatal"/>
</logging>


The result is as wanted:


public static final int init = 1
public static final int debug = 2
public static final int fatal = 4
public static final int ALL = 8
                                 
    Total is : 15


Hope this helped.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread