Tuesday, December 11, 2007

Odd and Even Numbers in XSL 1.0

Here's a way to tell if a number is odd or even in XSL 1.0, in this case as part of a snippet of code to switch CSS styles between rows in a search results set.
<xsl:attribute name="class">
<xsl:choose>
<!--returns true for odd counts-->
<xsl:when test="$number mod 2 = 1">highlight</xsl:when>
<!--returns true for even counts-->
<!--<xsl:when test="$number mod 2 = 0">nohighlight</xsl:when>-->
<xsl:otherwise>nohighlight</xsl:otherwise>
</xsl:choose>
</xsl:attribute>

No comments: