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 Search this Thread Display Modes
  #1 (permalink)  
Old June 16th, 2008, 11:59 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Loop?

I have the following JavaScript loop within an old ASP page that loops through the years from a starting year variable (1900) to the ending year variable (1990):
Code:
<%
//Year array
var strCurrDate = new Date();
strCurrYear = strCurrDate.getYear();
strVotingYear = strCurrYear - 18;//youngest voter year
strDiffYear = strVotingYear - 1900;//years from youngest minus oldest voters
//Voter year loop
var LoopNumber = strDiffYear;
for (i=1900;i<=strVotingYear;i++){
    Response.Write(i + "<br />");//test
    Response.Write("<option value='" + i + "'>" + i + "</option>");//test
}
%>
I'm wondering if I can do the same thing within XSLT with the use of a for:each method. I've been able to create the same variables with XSLT math, like this:
Code:
<xsl:param name="current_year" select="''" />
<xsl:param name="voting_year" select="$current_year - 18" />
<xsl:param name="diff_year" select="$voting_year - 1900" />
And maybe the XSLT code could look like this?
Code:
<select id="year" name="selectYear">
                                <option value="" selected="">- Year -</option>
    <xsl:for-each select="">

        <option value="???"><xsl:value-of select="???" /></option>

    </xsl:for-each>
</select>
Any help would be appreciated. Thanks.

KWilliams
Reply With Quote
  #2 (permalink)  
Old June 16th, 2008, 06:04 PM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

In XSLT 2.0 you can write

<xsl:for-each select="1900 to 1990">

There are also a lot of built-in functions and operators to do date/time arithmetic.

In XSLT 1.0 it's all rather hard work in comparison. The best way in XSLT 1.0 of iterating over a sequence of years like this is to write a recursive named template: pass it two parameters $startYear and $endYear, have it do the processing for $startYear and call itself to processing $startYear + 1.

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM
xslt when loop problem check XSLT 1 April 18th, 2008 08:47 AM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 02:27 PM
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 04:46 PM



All times are GMT -4. The time now is 01:57 AM.


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