Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 3rd, 2006, 04:17 PM
Authorized User
 
Join Date: Oct 2004
Location: Denver, CO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default create <ul> with alternating class on <li> element

using javascript or php this would be no problem, but not sure how to do this with xsl...

i have a list of nodes and for each node i want to create an <li> element. This is already done. However, i want the background color of each of the <li>'s to alternate. My thought is to set a variable, then reset that variable at the end of the loop, but not sure how that's done in xsl...i'd like something like this...

Code:
[list]
<li class="on"></li>
<li class="off"></li>
<li class="on"></li>
etc...
</ul>
Reply With Quote
  #2 (permalink)  
Old November 3rd, 2006, 04:53 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The basic technique is to use the position() function, assuming you have called xsl:apply-templates and selected all your li elements. You can use position() mod 2 to determine if it's an odd or even row and set the class accordingly.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old November 3rd, 2006, 06:07 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Don't think in terms of variables and loops, that kind of procedural thinking is too low-level for XSLT. You need to think of the output as a function of the input: the class is "on" for odd-numbered elements and "off" for even-numbered. That translates into

if (position() mod 2) = 1
  then "on"
  else "off"

Exactly how you express that depends on whether you're using 1.0 or 2.0 and where it fits in your stylesheet code, but I hope you get the idea.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 07:36 AM
<li></li> issue Adam H-W CSS Cascading Style Sheets 1 November 5th, 2007 06:41 AM
Links after the <ul> tag bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 May 15th, 2005 11:59 PM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 08:01 AM



All times are GMT -4. The time now is 05:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.