Click to See Complete Forum and Search --> : XSLT Loop?


kwilliams
06-16-2008, 01:00 PM
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" /><!-- 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.

rpgfan3233
06-16-2008, 01:21 PM
xsl:for-each is used for iteration over nodes, not over numeric ranges. Simply put, stick with the server-side generated way. There isn't really any use for such a thing in XSLT. After all, it only transforms markup; it doesn't generate data. :p

kwilliams
06-16-2008, 02:19 PM
That makes sense. So I'm now wondering if there's a way to use that code in a CDATA script within the XSLT doc. For instance, I have the following XSLT code that uses script that redirects the user when they make a selection from a jump-menu:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<script>
<![CDATA[
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
]]>
</script>
<select id="bycategory" name="selectCategory" onChange="MM_jumpMenu('parent',this,0)">
<option value="" selected="true">- Select a Category -</option>
<xsl:for-each select="categories/category">
<option value="{link}">
<xsl:value-of select="title" />
</option>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Would there be a way to use my already-existing script in this way? If so, could you please point me in the right direction? Thanks.