Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic colour swapping

Status
Not open for further replies.

stupotthesecond

Technical User
Jan 28, 2005
4
FR
I am generating a table using XSL from an XML document using an <xsl:for-each> loop.
What I need to do is alternate the background colour of each <tr> within this loop so I end up with one white row then one grey row, then one white row then etc... (somthing like the way the thread subjects are displayed in the forum)

Is there any way of doing this using XSL (with variables?)or do I need to use a javascript function, once the document has finished loading, to then change each <tr> 'bgcolor' attribute ?
 
There are several ways to do it. Here's one:

XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
	<row>
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
	<row>
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
	<row>
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
	<row>
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
</xml>

XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="/">
		<table>
			<tbody>
				<xsl:for-each select="xml/row">
					<tr style="background-color: white;">
						<xsl:if test="position() mod 2 = 1">
							<xsl:attribute name="style">background-color: gray;</xsl:attribute>
						</xsl:if>
						<td>
							<xsl:value-of select="item1"/>
						</td>
						<td>
							<xsl:value-of select="item2"/>
						</td>
					</tr>
				</xsl:for-each>
			</tbody>
		</table>
	</xsl:template>
</xsl:stylesheet>

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top