Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 30th, 2009, 11:09 PM
Authorized User
 
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
Default Alternating row color/style

Code:
<xsl:if test="position()  mod 2 = 1">
<tr bgcolor="#aaccff">
<td><xsl:value-of select="au_lname"/></td>
<td><xsl:value-of select="au_fname"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:if>

<xsl:if test="position()  mod 2 = 0">
<tr bgcolor="#ffccaa">
<td><xsl:value-of select="au_lname"/></td>
<td><xsl:value-of select="au_fname"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
I don't want to do it like the code above because I find it very redundant especially if you have a big xsl. Is there a better way to do this?

Like, if position mod 2 = 1, then apply this style to all TDs, otherwise apply this other style.

Thank you very much.
 
Old August 31st, 2009, 06:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Here's a XSLT2.0 solution:

<xsl:variable name="style" select="if position() mod 2 = 1 then '#aaccff' else '#ffccaa'"/>

<tr bgcolor="{$style}">
<td>...
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
kokoness (August 31st, 2009)
 
Old August 31st, 2009, 07:02 AM
Authorized User
 
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
Default

THanks Sam! I'll try it tomorrow at the office.
 
Old August 31st, 2009, 10:22 PM
Authorized User
 
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
Default

Hi Sam,

when I used this line (right after my for-each statement):

<xsl:variable name="style" select="if position() mod 2 = 1 then '#aaccff' else '#ffccaa'"/>

I keep getting an error:
This file is not valid: Invalid XPath expression

Or when source xml is opened directly in a browser:
Expected token 'eof' found 'NAME'. if -->position<--() mod 2 = 1 then '#aaccff' else '#ffccaa'


Any ideas why?

Thanks.

Last edited by kokoness; September 1st, 2009 at 01:36 AM..
 
Old September 1st, 2009, 04:08 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As I said, my solution was for XSLT 2.0. If you are using XSLT 1.0 then you will have to split the xpath expression out into xsl:choose, xsl:when and xsl:otherwise statements instead.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 1st, 2009, 06:19 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

For an input xml
Code:
<records>
<plan>
1
</plan>
<plan>
2
</plan>
<plan>
3
</plan>
<plan>
4
</plan>
</records>


the xslt code
Code:
<xsl:template match="records">
<xsl:apply-templates select="plan"></xsl:apply-templates> 
</xsl:template>
<xsl:template match="plan">
<xsl:variable name="style">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">#aaccff</xsl:when>
<xsl:otherwise>#ffccaa</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$style}">
<td><xsl:value-of select="position()"/></td>
</tr>
</xsl:template>


will give the output
Code:
<tr bgcolor="#aaccff">
<td>1</td>
</tr>
<tr bgcolor="#ffccaa">
<td>2</td>
</tr>
<tr bgcolor="#aaccff">
<td>3</td>
</tr>
<tr bgcolor="#ffccaa">
<td>4</td>
</tr>

__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
kokoness (September 2nd, 2009)
 
Old September 1st, 2009, 11:11 PM
Authorized User
 
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
Default

THanks mrame for the conversion.





Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP: Alternating Color Question borgymanotoy XSLT 1 July 16th, 2009 06:20 AM
style.color does not work in firefox baburman Other Programming Languages 0 January 29th, 2008 06:18 AM
Datalist Row Color rstelma ASP.NET 1.0 and 1.1 Professional 0 January 14th, 2008 08:44 PM
datagrid row color change berzegerol Visual Studio 2005 0 October 18th, 2006 06:33 AM
change row color vivek_pon VS.NET 2002/2003 3 April 13th, 2006 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.