[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Alternating Colors For Table Rows

Subject: Alternating Colors For Table Rows
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Thu, 21 Jun 2001 13:14:11 -0400
table alternating colors
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.