Re: [xsl] A Calendar Project...

Subject: Re: [xsl] A Calendar Project...
From: Jarkko Moilanen <Jarkko.Moilanen@xxxxxx>
Date: Mon, 16 Jun 2003 11:24:59 +0300 (EEST)
On Sat, 14 Jun 2003, Karl Stubsjoen wrote:

> Hello,
> I'm embarking on a calendar project... from the ground up.  So, I have the
> luxuray of putting it together any way I like : )
> I'm trying to determine the best XML data structure to suit XSLT processing.
>
> Thinking in terms of the XSLT processor:
>     1)  I need to be able to create standard 7 day week wide calendar by
> 30/31 day month view
>     2)  I need to be able to create a week view in a vertical and horizontal
> display format
>     3) As well an assortment of calendar views...
>
> With item # 1 above, the challenge is to create 7 TD's then a new TR... 7
> TD's a new TR and so on...
> The challenge in general is the grouping of different calendar parts, and
> the discovery of first day of week, etc...
>
> Has anyone else done such a project?

Well... here is one approach to do part of the calendar:

XML:
<?xml version="1.0"?>
<month start="5" days="30">
	<name>June 2001</name>
</month>


XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:output method="html" encoding="UTF-8" indent="yes"/>

<xsl:variable name="start" select="/month/@start"/>
<xsl:variable name="count" select="/month/@days"/>

<xsl:variable name="total" select="$start + $count - 1"/>
<xsl:variable name="overflow" select="$total mod 7"/>

<xsl:variable name="nelements">
	<xsl:choose>
		<xsl:when test="$overflow > 0"><xsl:value-of
select="$total + 7 - $overflow"/></xsl:when>
		<xsl:otherwise><xsl:value-of
select="$total"/></xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<xsl:template match="/">
	<html>
    <head><title><xsl:value-of select="month/name"/></title></head>

	<body bgcolor="lightyellow">

	<h1 align="center"><xsl:value-of select="month/name"/></h1>

	<table border="1" bgcolor="lightblue" noshade="yes"
align="center">
		<tr bgcolor="white">

<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>
		</tr>

		<xsl:call-template name="month"/>
	</table>

	</body></html>

</xsl:template>

<!-- Called only once for root -->
<!-- Uses recursion with index + 7 for each week -->
<xsl:template name="month">
	<xsl:param name="index" select="1"/>

	<xsl:if test="$index &lt; $nelements">
		<xsl:call-template name="week">
			<xsl:with-param name="index" select="$index"/>
		</xsl:call-template>

		<xsl:call-template name="month">
			<xsl:with-param name="index" select="$index + 7"/>
		</xsl:call-template>
	</xsl:if>

</xsl:template>

<!-- Called repeatedly by month for each week -->
<xsl:template name="week">
	<xsl:param name="index" select="1"/>
	<tr>
		<xsl:call-template name="days">
			<xsl:with-param name="index" select="$index"/>
			<xsl:with-param name="counter" select="$index +
6"/>
		</xsl:call-template>
	</tr>
</xsl:template>

<!-- Called by week -->
<!-- Uses recursion with index + 1 for each day-of-week -->
<xsl:template name="days">
	<xsl:param name="index" select="1"/>
	<xsl:param name="counter" select="1"/>

	<xsl:choose>
		<xsl:when test="$index &lt; $start">
			<td>-</td>
		</xsl:when>

		<xsl:when test="$index - $start + 1 > $count">
			<td>-</td>
		</xsl:when>

		<xsl:when test="$index > $start - 1">
			<td><a href="foo"><xsl:value-of select="$index -
$start + 1"/></td>
		</xsl:when>

	</xsl:choose>

	<xsl:if test="$counter > $index">
		<xsl:call-template name="days">
			<xsl:with-param name="index" select="$index + 1"/>
			<xsl:with-param name="counter" select="$counter"/>
		</xsl:call-template>
	</xsl:if>

</xsl:template>

</xsl:stylesheet>

Cheers,
Jarkko

> I will be building my XML data source from ASP, so I will script it on the
> fly in a suitable structure to be processed by XSLT.  I guess my real
> question is:  what would be a good XML structure for XSLT processing of a
> calendar?
>
> Here is a couple of ideas I had (very prelimary):
>
> <CAL D="2003-06-04">
>  <DATE D="2003-05-29" DOW="1" WK="100"/>
>  <DATE D="2003-05-30" DOW="2" WK="100"/>
>  <DATE D="2003-05-31" DOW="3" WK="100"/>
>  <DATE D="2003-06-01" DOW="4" WK="100"/>
>  <DATE D="2003-06-02" DOW="5" WK="100"/>
>  <DATE D="2003-06-03" DOW="6" WK="100"/>
>  <DATE D="2003-06-04" DOW="7" WK="100"/>
>     <DATE D="2003-06-05" DOW="1" WK="101"/>
> ....
> </CAL>
>
> Where CAL/@D is the current date (not necessarily todays date but the
> selected date)
> Where CAL/DATE/@DOW is the day-of-week (sunday..monday..tuesday.. ..)
> Where CAL/DATE/@WK is the week of the year
>
> Here is another format:
> <CAL D="2003-06-04">
>  <WEEK W="100">
>      <DATE D="2003-05-29" DOW="1" />
>      <DATE D="2003-05-30" DOW="2" />
>      <DATE D="2003-05-31" DOW="3" />
>      <DATE D="2003-06-01" DOW="4" />
>      <DATE D="2003-06-02" DOW="5" />
>      <DATE D="2003-06-03" DOW="6" />
>      <DATE D="2003-06-04" DOW="7" />
>   </WEEK>
>   <WEEK W="101">
>     <DATE D="2003-06-05" DOW="1">
>     ...
> </CAL>
>
>
> Again, the challeng (that I see) is grouping calendar dates into the
> different calendar views we are use to seeing.
> I've built a calendar before using ASP and recall the struggle I had of
> discovering first day of the month... what week day the first day of month
> fell on... I recall the "skipping" of non-current month dates, and so on...
>
> Any ideas would be appreciated!  I'm working on this all day today.  Oh, and
> if anyone is interested in collaboriting on this project, let me know.
>
> Karl
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>


****************************************************************
Jarkko Moilanen          "Erehtyminen on inhimillista,
Researcher                mutta todella suuret mokat
jm60697@xxxxxx            vaativat tietokoneen käyttöä."
www.uta.fi/~jm60697
GSM: +358 50 3766 927
****************************************************************
* ITCM | Information Technology and Crisis Management
* http://www.itcm.org
****************************************************************






 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread