What is the easiest way to alternate row colors in an HTML table (a.ka. striping). Most of my tables are created in XSL templates like the following, with the table, thead, etc.. defined in another template.
<xsl:template match="typ:info">
<tr>
<td>
<xsl:value-of select="typ:dateAccessed" />
</td>
<td>
<xsl:value-of select="typ:loginId" />
</td>
</tr>
</xsl:template>
My preference is to use alternating classes on the elements.
If you must produce hard-coded colors in HTML:
With today's browsers you are much better off using CSS and
tr:nth-child(odd)
.This results in less hassle on the XSLT side, much cleaner HTML markup - and it's compatible with client-side table sorting and -filtering.
Use an XSL:When and compare position mod 2 to get odd or even rows to alter classes when needed like:
EDIT: Getting my odd/even the right way around sigh
May we want to change only the class name instead, when can choose inside a variable to enable change its inner value. Something like this:
With this, we are able to change the variable name and then we can change for example, a div class content.
Regards!
You could also use css3.
Supported as of IE9 an for quite some time by all the other browsers.