Re: [xsl] How to create XSL for CALENDAR/MONTH layout?

Subject: Re: [xsl] How to create XSL for CALENDAR/MONTH layout?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 5 Jan 2001 18:35:53 +0000
Hi David,

> I have a calendar that displays 12 months of a year. Currently, all
> 12 months display across the page as one row. Instead, I'd like to
> arrange them a 3 rows with 4 months in each row. Any ideas how to do
> this: <tr>4 MONTHS HERE</tr>?

This is a grouping-by-position problem: you want to group the months
according to their position within the CALENDAR element.

As with any grouping problem, you can break it down into two steps:
1. finding the first node in a group
2. processing the group

The usual way of finding the first node in a group based on position
use the mod operator on the position of the node.  If you want to
group into groups of 4, then the position of the first node in each
group mod 4 will equal 1.  In your case, you can use the XPath:

  MONTH[position() mod 4 = 1]

to select the months that are first in each row.  I'd probably select
these by applying templates in 'row' mode inside the
CALENDAR-matching template:

<xsl:template match="CALENDAR">
  <xsl:apply-templates select="MONTH[position() mod 4 = 1"
                       mode="row" />

Then create a template that matches MONTHs in 'row' mode. Because
you've only selected the first in the group, this template will only
fire once per row. This template needs to create a row and then apply
templates to each of the months in the group. The group of months
consists of the MONTH you're currently on and its next 3 siblings,

  . | following-sibling::MONTH[position() &lt; 4]

So it should look something like:

<xsl:template match="MONTH" mode="row">
      select=". | following-sibling::MONTH[position() &lt; 4]" />

This will then use the MONTH-matching template that you already have.

If you want to, you can separate out the number of months that you
want in each row into a variable or parameter that you can change
during development, or even let the user change dynamically.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread