Search

I'm trying to generate a select list of years from 1900-$this-year. Does anyone know the XSLT for this?

E.g.

<select name="year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
....
<option value="1900">1902</option>
<option value="1900">1901</option>
<option value="1900">1900</option>
</select>

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 &lt; $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.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details