Subject: [xsl] Alternating Colors For Table Rows From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx> Date: Thu, 21 Jun 2001 13:14:11 -0400 |
People often ask how to alternate colors in an html table - it's come up several times lately. The method is simple, and it's pretty widely known. You choose the color based this test: position() mod 2 = 0 This assumes that the context node for position() is a row element, of course. How would you do it if you wanted to alternate in groups of two, three, or more rows rather than every other one? For example, blue,blue,yellow,yellow,blue,blue,...? In a procedural language, you'd just keep updating a color status flag, but you can't do that in xslt (and using recursive calls to pass the color status seems overkill for this little task). I devised a neat way to do this, and I thought I'd share it. If the repeat index is in $n, here's the test: position() mod $n = position() mod (2$n) Simple, eh? Here's how it works. Say n=3. then position() position() mod $n position() mod (2$n) 0 0 0 1 1 1 2 2 2 ----------------------------------------------------------- 3 0 3 4 1 4 5 2 5 --------------------------------------------------------- 6 0 0 7 1 1 8 2 2 --------------------------------------------------------- 9 0 3 etc. See how it works? In practice, position() is 1-based, not zero-based, so you have to use (position()-1). In fact, I found that I actually had to use number(position()) -1. With this scheme, you can pass in a parameter for the repeat count without having to change the code for each different value. Enjoy... Tom P XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Difference between "/" an, Michael Kay | Thread | Re: [xsl] Alternating Colors For Ta, Thomas B. Passin |
Re: [xsl] XSLT Problem (How to comp, David Carlisle | Date | Re: [xsl] XSLT Problem (How to comp, Thomas B. Passin |
Month |