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 November 3rd, 2006, 04:17 PM
Authorized User
 
Join Date: Oct 2004
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>
 
Old November 3rd, 2006, 04:53 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old November 3rd, 2006, 06:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06: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 10: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 07:01 AM





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