RE: [xsl] alternating bg colour in nested data

Subject: RE: [xsl] alternating bg colour in nested data
From: "Andrew Welch" <andrew@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 16 Jan 2002 12:05:23 -0000
Thanks Ken,

Now that works, is it possible to make it match any number of groups? (make
it generic)

Say there could just be group1 and group2, or there could be twenty... with
the only known structure being that the next group is always the last
element in the last occurring previous group.

Ive had a go a this and crashed and burned so far :(

cheers

andrew



-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of G. Ken Holman
Sent: Tuesday, January 15, 2002 9:18 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] alternating bg colour in nested data


At 2002-01-15 17:51 +0000, Andrew Welch wrote:
>Im trying to get alternating table row background colours from nested xml.
>...
>I currently get alternating row colours using this kind of template for
each
>group:
>
><xsl:template match="group1">
><xsl:variable name="position" select="position()" />

The above is redundant ...

><xsl:choose>
>   <xsl:when test="$position mod 2 = 1">

You could have used test="position() mod 2 = 1"

>     <xsl:call-template name="row_even"/>
>   </xsl:when>
>   <xsl:otherwise>
>     <xsl:call-template name="row_odd"/>
>   </xsl:otherwise>
></xsl:choose>
><xsl:apply-templates select="group2"/>
></xsl:template>
>
>Each template is virtually identical apart from the 'groupX' number.

I have a suggestion for that as well.

>Also,
>occasionally two adjacent rows share the same colour (where group changes
>and last elem pos was even)

You'll have to keep track of the running count of all rows generated.

Now I don't know what you are doing in each row, but the example below
shows how I keep track of the running row count in a variable that is
passed.

Note also how I was able to reuse the same template rather than making
numerous "groupX" templates ... I am assuming the structure is as regular
as you imply it is.  If there are no children of the given name, it isn't
an error, so asking for all possible children at each level will only give
you the children that are at that level.

I hope this helps.

............................. Ken



T:\ftemp>type andrew.xml
<group1>
   <data1/>
   <data2/>
   <group2>
     <data1/>
     <data2/>
   </group2>
   <group2>
     <data1/>
     <data2/>
     <group3>
       <data1/>
       <data2/>
     </group3>
     <group3>
       <data1/>
       <data2/>
       <group4>
         <data1/>
         <data2/>
       </group4>
     </group3>
   </group2>
</group1>

T:\ftemp>type andrew.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
   <table>
     <xsl:apply-templates select="group1"/>
   </table>
</xsl:template>


<xsl:template match="group1|group2|group3|group4">
<xsl:param name="last" select="0"/>
<xsl:choose>
   <xsl:when test="( $last + position() ) mod 2 = 1">
     <xsl:call-template name="row_even"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:call-template name="row_odd"/>
   </xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="group2|group3|group4">
   <xsl:with-param name="last" select="$last + last()"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template name="row_even">
   <tr bgcolor="#cccccc">
     <xsl:for-each select="*[not(starts-with(name(),'group'))]">
       <td><xsl:value-of select="concat(name(..),' ',name(.))"/></td>
     </xsl:for-each>
   </tr>
</xsl:template>

<xsl:template name="row_odd">
   <tr>
     <xsl:for-each select="*[not(starts-with(name(),'group'))]">
       <td><xsl:value-of select="concat(name(..),' ',name(.))"/></td>
     </xsl:for-each>
   </tr>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt andrew.xml andrew.xsl andrew.htm

T:\ftemp>type andrew.htm
<?xml version="1.0" encoding="utf-8"?>
<table>
<tr bgcolor="#cccccc">
<td>group1 data1</td>
<td>group1 data2</td>
</tr>
<tr>
<td>group2 data1</td>
<td>group2 data2</td>
</tr>
<tr bgcolor="#cccccc">
<td>group2 data1</td>
<td>group2 data2</td>
</tr>
<tr>
<td>group3 data1</td>
<td>group3 data2</td>
</tr>
<tr bgcolor="#cccccc">
<td>group3 data1</td>
<td>group3 data2</td>
</tr>
<tr>
<td>group4 data1</td>
<td>group4 data2</td>
</tr>
</table>


--
Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002
-               (Early-bird date for discounts is this Friday!)

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:            2002-01-18,02-11,12,13,15,18,21,
-                                03-11,14,15,18,19,04-08,09,10,12


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



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


Current Thread