Alternate colors for line groups !!!

admin

Administrator
Staff member
I spent most of the day trying to achieve this... As it seems I succeded at last, I feel so proud of myself that I really have to share it here :D

My XML test file:


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href=http://www.webdeveloper.com/forum/archive/index.php/"LignesAlternees.xsl"?>
<File>
<Item>
<Grp>a</Grp><Txt>Line 1</Txt>
</Item>
<Item>
<Grp>a</Grp><Txt>Line 2</Txt>
</Item>
<Item>
<Grp>b</Grp><Txt>Line 3</Txt>
</Item>
<Item>
<Grp>c</Grp><Txt>Line 4</Txt>
</Item>
<Item>
<Grp>c</Grp><Txt>Line 5</Txt>
</Item>
<Item>
<Grp>c</Grp><Txt>Line 6</Txt>
</Item>
<Item>
<Grp>d</Grp><Txt>Line 7</Txt>
</Item>
<Item>
<Grp>d</Grp><Txt>Line 8</Txt>
</Item>
<Item>
<Grp>e</Grp><Txt>Line 9</Txt>
</Item>
</File>


and my XSL stylesheet:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1"/>

<xsl:template match="/">
<html>
<head><title>Alternate colors</title></head>
<body>
<h3>ALTERNATE COLOR FOR LINE GROUPS</h3>
<xsl:apply-templates select="File"/>
</body>
</html>
</xsl:template>

<xsl:template match="File"><center>
<table cellpadding="3" cellspacing="3" border="1" bgcolor="gold" width="200px">
<thead>
<tr><th align="center" width="50%">Group</th>
<th align="center" width="50%">Content</th></tr>
</thead>
<tbody>
<xsl:apply-templates select="Item"/>
</tbody>
</table>
</center>
</xsl:template>

<xsl:template match="Item">
<xsl:choose>
<xsl:when test = "(count(preceding-sibling::*[Grp != following-sibling::*[position()=1]/Grp]) mod 2) = 1">
<tr align="center" bgcolor="lightblue">
<td width="50%"><b>
<xsl:value-of select="Grp"/>
</b></td>
<td width="50%"><xsl:value-of select="Txt"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr align="center" bgcolor="lightyellow">
<td><b>
<xsl:value-of select="Grp"/>
</b></td>
<td><xsl:value-of select="Txt"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


Hurrah !
 
Back
Top