|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: 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: The above is redundant ... <xsl:choose> <xsl:when test="$position mod 2 = 1"> You could have used test="position() mod 2 = 1"
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 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>
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








