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 June 16th, 2008, 11:59 AM
Join Date: Jul 2005
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):
//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:
<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?
<select id="year" name="selectYear">
                                <option value="" selected="">- Year -</option>
    <xsl:for-each select="">

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

Any help would be appreciated. Thanks.

Old June 16th, 2008, 06:04 PM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

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
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

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

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