How to I "stripe" columns using XSLT

Given an XML "table" with an arbitrary and unknown number of columns, what is the best way to handle column even/odd formatting so that the results are striped columns?
Who is Participating?
Geert BormansConnect With a Mentor Information ArchitectCommented:
well, given the question clearly says "using XSLT", I can offer you a XSLT solution that works regardless of the browser version

the trick is to use "mod" on position of cells inside the row (I assume your XML model is row based not column based, if it is column based, you need to pivot in order to get html anyway)

on the cell you do this (assuming an apply templates for the cells only, and assuming cell as the cell element name in your XML)
<xsl:template match="cell">
<xsl:attribute name="class">
<xsl:when test="position() mod 2 = 1">cell-odd</xsl:when>
<xsl:value-of select="."/>

so you can have in your css a style for cell-even and cell-odd, setting the background color
	tr td:nth-child(odd){ 
		background: blue;
	tr td:nth-child(even){
		background: green;

Open in new window

This is CSS3 so won't work in IE8 and lower.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.