Jump to content

XSLT Loop?


kwilliams

Recommended Posts

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 arrayvar strCurrDate = new Date();strCurrYear = strCurrDate.getYear();strVotingYear = strCurrYear - 18;//youngest voter yearstrDiffYear = strVotingYear - 1900;//years from youngest minus oldest voters//Voter year loopvar 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" /><!-- youngest voter year --><xsl:param name="diff_year" select="$voting_year - 1900" /><!-- years from youngest minus oldest voters -->

And maybe the XSLT code could look like this?

<select id="year" name="selectYear">								<option value="" selected="">- Year -</option>	<xsl:for-each select="">		<!-- ???Not sure what to do here??? -->		<option value="???"><xsl:value-of select="???" /></option>			</xsl:for-each></select>

Any help would be appreciated. Thanks.

Link to comment
Share on other sites

You can't do it with for-each, as variables cannot be changed within a template. But you can do it with a template that you'd call, and pass a variable to it that will change with each call. Something like:

<xsl:param name="current_year" select="''" /><xsl:param name="voting_year" select="$current_year - 18" /><!-- youngest voter year --><xsl:param name="diff_year" select="$voting_year - 1900" /><!-- years from youngest minus oldest voters --><xsl:template match="/">...<select id="year" name="selectYear">	<option value="" selected="">- Year -</option>		<xsl:call-template name="years">			<xsl:with-param name="voting_year" select="$voting_year"/>			<xsl:with-param name="diff_year" select="$diff_year"/>		</xsl:call-template></select></xsl:template><xsl:template name="years"><xsl:param name="voting_year" /><xsl:param name="diff_year" /><option value="{$diff_year}"><xsl:value-of select="$diff_year"/></option><xsl:if test="$diff_year <= $voting_year">		<xsl:call-template name="years">			<xsl:with-param name="voting_year" select="$voting_year"/>			<xsl:with-param name="diff_year" select="$diff_year + 1"/>		</xsl:call-template></xsl:if></xsl:template>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...