【发布时间】:2011-11-16 18:42:51
【问题描述】:
我有一个 XSLT,我想在其中交替输出表的行颜色。我知道你可以使用这个例子中的代码:
<table>
<tr>
<td>Name</td>
<td>ID</td>
</tr>
<xsl:for-each select="//Book">
<xsl:variable name="altColor">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when>
<xsl:otherwise>#D3DFEE</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$altColor}">
<td><xsl:value-of select="current()/@name"/></td>
<td><xsl:value-of select="current()/@ID"/></td>
</tr>
</xsl:for-each>
</table>
这很好用,但是我有一些情况需要在 for-each 中包含一些 if 语句,例如。
<table>
<tr>
<td>Name</td>
<td>ID</td>
</tr>
<xsl:for-each select="//Book">
<xsl:variable name="altColor">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when>
<xsl:otherwise>#D3DFEE</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="current()/@ID > 10000 and current/@ID < 6000">
<tr bgcolor="{$altColor}">
<td><xsl:value-of select="current()/@name"/></td>
<td><xsl:value-of select="current()/@ID"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
然后它不起作用,因为它可能会跳过 for-each 中某个位置的项目,我最终会随机交替行颜色,或者它可能从不正确的位置开始,其中行以不正确的颜色开始交替。
我尝试添加一个 xsl:sort,但这并不能真正解决问题。有没有办法避免这种障碍?
【问题讨论】:
标签: xslt