Generate list of years in XSLT
This is an open discussion with 3 replies, filed under XSLT.
Search
Xpathr does not seem to work today, so it might not work out of the box (haven't tested it), but here is my take:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <xsl:call-template name="years"> <xsl:with-param name="current" select="'1900'"/> </xsl:call-template> </xsl:template> <xsl:template name="years"> <xsl:param name="current"/> <xsl:param name="stop" select="$this-year"/> <xsl:if test="$current < $stop"> <option value="$current"><xsl:value-of select="$current"/></option> <xsl:call-template name="years"> <xsl:with-param name="current" select="$current + 1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
Does this make sense?
edit: Oh, you can of course also change the order by going the other way around: passing in the stop parameter and counting down as long as current
is greater than stop
.
Thanks Huib. Works like a dream and is a handy learning tool.
It just turned out to be a very handy learning tool for me too. Thanks a lot!
Create an account or sign in to comment.
I'm trying to generate a select list of years from 1900-
$this-year
. Does anyone know the XSLT for this?E.g.