Re: Alternating table row colors with recursion

Subject: Re: Alternating table row colors with recursion
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Tue, 28 Nov 2000 11:37:52 -0800
I recently had a similar problem whose solution worked
for me, but is not necessarily the most performant option
available. It might get some other thoughts spinning though
so here goes...

I have a requirements document like this:

     <req title="ta">
       <req title="xy"/>
       <req title="zz">
         <req title="pp"/>
     <req title="qq">
       <req title="bb">
         <req title="mm"/>
         <req title="jj"/>
       <req title="ww"/>

Which I wanted to print out with a global-alternating
row colors for rows like:

Color Text
----- ---------------------------
[0]   1. ta
[1]      1.1 xy
[0]      1.2 zz
[1]          1.2.1 pp
[0]   2. qq
[1]      2.1 bb
[0]          2.1.1 mm
[1]          2.1.2 jj
[0]      2.2 ww

I didn't want the coloring to *restart* with each
subnode's children list.

I used a brute-force approach of doing:

  <xsl:variable name="global-position"
                select="count(preceding::req) +
                        count(ancestor::req)  + 1"/>

which gave me the overall effective linear "row number"
regardless of the level of the tree, and then I used
the standard:

   <tr class="c{$global-position mod 2}">

to create <table> rows whose CSS class name alternates
between "c0" and "c1" for the even and odd colors.

This counting approach was just what I thought of, but
you might also use the output of <xsl:number> for
the current <req> and do some kind of calculation
on the "2.1.2" value to derive the relative row 
number without counting again (but I haven't thought 
that one through yet)

Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly

 XSL-List info and archive:

Current Thread