produce colors in alternate rows in the table

M

Matt

I want to produce colors in alternate rows in the table.

The following will produce blue color on every row. So my problem is to figure
out to count the records. If it is odd record, then do <tr bgcolor="blue">,
otherwise, don't do anything, and it will output colors in alternate
rows. I tried to use count, but doesn't work.

Any ideas? Please advise.

<xsl:for-each select="/authors/author">
<tr bgcolor="blue">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="url"/></td>
</tr>

<authors>
<author>
<name>author1</name>
<url>http://url1.com</url>
</author>
<author>
<name>author2</name>
<url>http://url2.com</url>
</author>
<author>
<name>author3</name>
<url>http://url3.com</url>
</author>
<author>
<name>author4</name>
<url>http://url4.com</url>
</author>
</authors>
 
M

Martin Honnen

Matt said:
I want to produce colors in alternate rows in the table.

The following will produce blue color on every row. So my problem is to figure
out to count the records. If it is odd record, then do <tr bgcolor="blue">,
otherwise, don't do anything, and it will output colors in alternate
rows. I tried to use count, but doesn't work.

Any ideas? Please advise.

<xsl:for-each select="/authors/author">
<tr bgcolor="blue">

Use position e.g.
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2 =
0"><xsl:text>red<</xsl:text></xsl:when>
<xsl:eek:therwise><xsl:text>blue</xsl:text></xsl:eek:therwise>
</xsl:choose>
</xsl:attribute>
 
J

Joris Gillis

Hi,

I think you should use CSS where it is possible.
It makes the whole more controllable and even smaller:

<xsl:template match="/">
<html>
<head>
<style type="text/css">
tr.r0 {background-color:red}
tr.r1 {background-color:blue}
</style>
</head>
<body>
<table>
<xsl:apply-templates select="//authors/author"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="author">
<tr class="r{position() mod 2}">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="url"/></td>
</tr>
</xsl:template>


regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top