Increment Variable count in for-each in XSLT 1.0

2019-09-09 03:46发布

问题:

I want to Increment variable in for-each loop. Here is my code.

<xsl:variable name="i" select="1" />
<xsl:variable name="oddEven" select="1" />
<xsl:for-each select="//ProfileBR">
    <xsl:variable name="j" select="$i + 1" />
    sharad j :: <xsl:value-of select="$j"></xsl:value-of>
    <xsl:variable name="iBR" select="substring(//BRValue,$i,1)" />
    <xsl:variable name="jBR" select="substring(//BRValue,$j,1)" />
    <xsl:if test="$iBR='1' or $jBR='1'">
        <xsl:choose>
            <xsl:when test="$oddEven='1'">
                <tr class="sbListOddCell">
                    <xsl:call-template name="JobInfoSection">
                        <xsl:with-param name="ii" select="$i"/>
                        <xsl:with-param name="jj" select="$j"/>
                        <xsl:with-param name="iiBR" select="$iBR"/>
                        <xsl:with-param name="jjBR" select="$jBR"/>
                    </xsl:call-template>
                </tr>
                <xsl:variable name="oddEven" select="0" />
            </xsl:when>
            <xsl:otherwise>
                <tr class="sbListEvenCell">
                    <xsl:call-template name="JobInfoSection">
                        <xsl:with-param name="ii" select="$i"/>
                        <xsl:with-param name="jj" select="$j"/>
                        <xsl:with-param name="iiBR" select="$iBR"/>
                        <xsl:with-param name="jjBR" select="$jBR"/>
                    </xsl:call-template>
                </tr>
                <xsl:variable name="oddEven" select="1" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
    <xsl:variable name="i" select="$j + 1" />
</xsl:for-each>

I want to increment i and j in every iteration but it ends up with 3 and 2 respectively after each iteration.

How can I increment i and j.

Thanks, Sharad

回答1:

Use position() to get the count of the iteration inside the for-each like this:

<xsl:variable name="j" select="$i + position()" />


标签: xslt