Increment Variable count in for-each in XSLT 1.0

徘徊边缘 提交于 2020-01-17 01:23:30

问题


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()" />


来源:https://stackoverflow.com/questions/27147255/increment-variable-count-in-for-each-in-xslt-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!