This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Alternating table row colors with recursion


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:

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

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
http://www.oreilly.com/catalog/orxmlapp/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]