xslt for-each - 将类添加到第1,第4,第7,第10 ......项

时间:2014-02-14 11:57:50

标签: html xml xslt xslt-1.0

对于xslt-1.0,如何修改下面的代码,以便将third类添加到第一个,第四个,第四个等inner-div个元素中?

<div id="myDiv">
    <xsl:for-each select="key('category',  $var1)">
        <div class="inner-div">
            sometext
        </div>
    </xsl:for-each>
</div>

所以它是:

<div id="myDiv">
        <div class="inner-div third">
            sometext
        </div>
        <div class="inner-div">
            sometext
        </div>
        <div class="inner-div">
            sometext
        </div>
        <div class="inner-div third">
            sometext
        </div>
        <div class="inner-div">
            sometext
        </div>
        <div class="inner-div">
            sometext
        </div>
        <div class="inner-div third">
            sometext
        </div>
</div>

谢谢。

1 个答案:

答案 0 :(得分:3)

您需要对position()

进行模运算
<div id="myDiv">
    <xsl:for-each select="key('category',  $var1)">
        <div>
            <xsl:attribute name="class">
                <xsl:text>inner-div</xsl:text>
                <xsl:if test="(position() mod 3) = 1"> third</xsl:if>
            </xsl:attribute>
            sometext
        </div>
    </xsl:for-each>
</div>