对于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>
谢谢。
答案 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>