<Re: Increment

"Igor Tandetnik" <[email protected]> Wed, 1 Aug 2007 11:25:06 -0400
Newsgroups gmane.comp.mozilla.devel.layout.xslt
Message-ID <[email protected]>
IVK <[email protected]> wrote:
> Hello, I want to increment a variable in for-each loop. I could use
> "<xsl:value-of select="position()" />" if it is a direct one. But my
> requirement can be summarized as follows:
>
> declare count
> for-each()
> {
> if(something)
> {
> increment count
> }
> else
> {
> nothing
> }
> }

You'll have to switch from iteration to a recursive algorithm. Something 
like this:

<template name="loop-body">
    <param name="node-list"/>
    <param name="current-node-pos"/>
    <param name="count"/>

    <case>
        <when test="$current-node-pos &gt; last($node-list)>
            <!-- end of loop - do nothing -->
        </when>
        <when test="something">
            <call-template name="loop-body">
                <with-param name="node-list" select="$node-list"/>
                <with-param name="current-node-pos" 
select="$current-node-pos+1"/>
                <with-param name="count" select="$count+1"/>
            </call-template>
        </when>
        <otherwise>
            <call-template name="loop-body">
                <with-param name="node-list" select="$node-list"/>
                <with-param name="current-node-pos" 
select="$current-node-pos+1"/>
                <with-param name="count" select="$count"/>
            </call-template>
        </otherwise
    </case>
</template>

<!-- start the ball rolling -->
<call-template name="loop-body">
    <with-param name="node-list" select="..."/>
    <with-param name="current-node-pos" select="1"/>
    <with-param name="count" select="1"/>
</call-template>

Igor Tandetnik