Search

Hi all,

I'm trying to create a list of date links on my archive page between a certain date and either $this-month or ideally the month of the most recent blog post. For example the list might look something like this:

<ul>
    <li><a href="{root}/archive/2009/04">April 2009</a></li>
    <li><a href="{root}/archive/2009/03">March 2009</a></li>
    <li><a href="{root}/archive/2009/02">February 2009</a></li>
    <li><a href="{root}/archive/2009/01">January 2009</a></li>
    <li><a href="{root}/archive/2008/12">December 2008</a></li>
</ul>

etc... I'm not sure what the best way of achieving this is. I've tried using recursion to output the numbers which I could then convert to text based dates, but I keep running into xslt errors.

This is what I have tried:

<xsl:template name="date-recursion">
    <xsl:param name="dr-year" select="$this-year" />
    <xsl:param name="dr-month" select="$this-month" />

    <li><xsl:value-of select="$dr-year" />-<xsl:value-of select="$dr-month" /></li>

    <xsl:if test="$dr-month &gt; 0 and $dr-year &gt; 2007">
        <xsl:choose>
            <xsl:when test="$dr-month = 1">
                <xsl:call-template name="date-recursion">
                    <xsl:with-param name="dr-month" select="12" />
                    <xsl:with-param name="dr-year" select="$dr-year - 1" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="date-recursion">
                    <xsl:with-param name="dr-month" select="$dr-month - 1" />
                </xsl:call-template>
            </xsl:otherwise>    
        </xsl:choose>   
    </xsl:if>
</xsl:template>

You might want to try using more than one recursive template: one for years, one for months. It would look like this (including the format-number function to maintain the double digit month format):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

<xsl:template match="/">
    <xsl:call-template name="year-recursion">
        <xsl:with-param name="dr-month" select="$this-month" />
        <xsl:with-param name="dr-year" select="$this-year" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="year-recursion">
    <xsl:param name="dr-year"/>
    <xsl:param name="dr-month"/>

    <xsl:if test="$dr-year &gt; 2007">
        <xsl:call-template name="month-recursion">
            <xsl:with-param name="dr-month" select="$dr-month" />
            <xsl:with-param name="dr-year" select="$dr-year" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="month-recursion">
    <xsl:param name="dr-year"/>
    <xsl:param name="dr-month"/>

    <li><xsl:value-of select="$dr-year" />-<xsl:value-of select="format-number($dr-month, '00')" /></li>

    <xsl:choose>
        <xsl:when test="$dr-month &gt; 1">
            <xsl:call-template name="month-recursion">
                <xsl:with-param name="dr-year" select="$dr-year" />
                <xsl:with-param name="dr-month" select="$dr-month - 1" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="year-recursion">
                <xsl:with-param name="dr-year" select="$dr-year - 1" />
                <xsl:with-param name="dr-month" select="12" />
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

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