xsl-list
[Top] [All Lists]

Re: calculating pow of numbers and a final sum

2003-01-22 03:34:22
Hi Andreas,

I think that this is one of those occasions wher you do need to use recursion - I can't (at this time of morning) see any way of doing it with a simple XPath maths expression, or even XPath numeric expressions inside a loop.

Try something like this -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="text"/>
   <!-- case conversion variables -->
   <xsl:variable name="abc">abcdefghijklmnopqrstuvwxyz</xsl:variable>
   <xsl:variable name="ABC">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
   <!-- kick off recursive transform -->
   <xsl:template match="logging">
       <xsl:apply-templates select="target[1]"/>
   </xsl:template>
   <!-- simple tail-end recursive transform -->
   <xsl:template match="target">
       <xsl:param name="base" select="1" />
public static final int <xsl:value-of select="translate(@name, $abc, $ABC)" /> = <xsl:value-of select="$base" />;
       <xsl:apply-templates select="following-sibling::target[1]">
           <xsl:with-param name="base" select="$base * 2" />
       </xsl:apply-templates>
   </xsl:template>
</xsl:stylesheet>

Using recursion to step through a sequence of elements, one by one, is in fact a helpful in many cased where you need to perform arbitrary cumulative operations which can't be done using numeric operators, sum(), position() or xsl:number, for instance in reports.

Francis.

--
"Never mind manoeuvre, go straight at 'em." - Admiral Horatio Nelson



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