1

I would like to increment my global variable tmp_balance_virt in for-each loop but in each loop tmp_balance_virt (a=a+b) is resetting to its initial value, how to make it works?

<xsl:variable name="tmp_balance_virt" select="/doc/plot/@st_balance_virt" /> //first value  
...
<xsl:if ...>
...
<xsl:for-each
    select="/doc/plot/cfc/pl.cashflow.rabean.TransactionsData[type=0]">
                <xsl:variable name="tmp_balance_virt" select="value + $tmp_balance_virt" />
                [
                <xsl:value-of select="date_ms" />
                ,
                <xsl:value-of select="$tmp_balance_virt" />
                ],
</xsl:for-each>

EDIT: That is what works for me.

        <xsl:for-each
            select="/doc/plot/cfc/pl.cashflow.rabean.TransactionsData[type=1]">
            <xsl:variable name="PREC"
                select="count(preceding-sibling::pl.cashflow.rabean.TransactionsData[type=1])+2" />
            [
            <xsl:value-of select="date_ms" />
            ,
            <xsl:value-of
                select="sum(/doc/plot/cfc/pl.cashflow.rabean.TransactionsData[type=1][position() &lt; $PREC]/value) + $tmp_balance_real" />
            ],
        </xsl:for-each>
Damian
  • 2,930
  • 6
  • 39
  • 61

2 Answers2

1

There is an answer for this here:

In XSLT how do I increment a global variable from a different scope?

Not exactly what you are after but it has some good suggestions that should help you out

Community
  • 1
  • 1
Steve Newstead
  • 1,223
  • 2
  • 10
  • 20
1

XSLT is a functional language, which means that variables are immutable. Once set, they can't be changed. This means you will need to change your approach, and think in a more functional way.

In this case, you could probably make use of the sum function, and do something like this to initialise the variable

<xsl:variable 
   name="tmp_balance_virt" 
   select="/doc/plot/@st_balance_virt
     + sum(/doc/plot/cfc/pl.cashflow.rabean.TransactionsData[type=0]/value)" />
tzot
  • 92,761
  • 29
  • 141
  • 204
Tim C
  • 70,053
  • 14
  • 74
  • 93