[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Building a calendar

Subject: Re: Building a calendar
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 2 Sep 1999 14:15:56 +0100 (BST)
week calendar div stylesheet
  <MONTH start="4" end="30"> <!-- start is the day of the week that the month

It seems that the stylesheet probably ought to know what day any given
month starts on without relying on a user to know this.

The following stylesheet probably makes this calculation, although
the formula is from memory so if it falls over Y2K then don't blame
me:-)

It takes input something like

<test>

<cal year="1998" month="2" />

<cal year="1999" month="2" />

<cal year="2000" month="1" />
<cal year="2000" month="2" />
<cal year="2000" month="3" />

</test>

and produces calendars for the specified months.
It could be integrated with the stylesheet already posted that adds
information about events to the days, but that's left as an excercise
for the reader.

Comments in code are I'm afraid up to my usual standard.

David

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head/>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>



<xsl:template name="j1ly">
<xsl:param name="year" />
<xsl:value-of select=   "(1 + $year +
    (floor($year div 4)) -
    (floor($year div 100)) +
    (floor($year div 400))) mod 7"/>
</xsl:template>

<xsl:template match="cal">
<xsl:variable name="j1">
<xsl:call-template name="j1ly">
<xsl:with-param name="year" select="@year - 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="j1ny">
<xsl:call-template name="j1ly">
<xsl:with-param name="year" select="@year "/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="end">
  <xsl:call-template name="mlength">
  <xsl:with-param name="month" select="@month"/>
  <xsl:with-param name="leap" select="$j1ny = (($j1+2 )mod 7)"/>
  </xsl:call-template >
</xsl:variable>

<xsl:variable name="moff">
  <xsl:call-template name="moffset">
  <xsl:with-param name="month" select="@month"/>
  </xsl:call-template>
</xsl:variable>

<xsl:variable name="start">
<xsl:choose>
  <xsl:when test="($j1ny = (($j1+2 )mod 7)) and (@month > 2)">
  <xsl:value-of select="($j1 + $moff + 1) mod 7"/>
  </xsl:when>
  <xsl:otherwise>
  <xsl:value-of select="($j1 + $moff) mod 7"/>
  </xsl:otherwise> 
</xsl:choose>
</xsl:variable>

<table>
<tr>
<th colspan="7" align="center">
<xsl:value-of select="@year"/>/<xsl:value-of select="@month"/>
</th>
</tr>
<tr>
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
<xsl:call-template name="week">
  <xsl:with-param name="start" select="1 - $start"/>
</xsl:call-template>
<xsl:call-template name="week">
  <xsl:with-param name="start" select="8 - $start"/>
</xsl:call-template>
<xsl:call-template name="week">
  <xsl:with-param name="start" select="15 - $start"/>
</xsl:call-template>
<xsl:call-template name="week">
  <xsl:with-param name="start" select="22 - $start"/>
</xsl:call-template>
<xsl:call-template name="week">
  <xsl:with-param name="start" select="29 - $start"/>
  <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="week">
  <xsl:with-param name="start" select= "36 - $start"/>
  <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
</table>
</xsl:template>

<xsl:template name="week">
  <xsl:param name="start"/>
  <xsl:param name="end" select="31"/>
<tr>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="1+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="2+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="3+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="4+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="5+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
<xsl:call-template name="day">
 <xsl:with-param name="val" select="6+$start"/>
 <xsl:with-param name="end" select="$end"/>
</xsl:call-template>
</tr>
</xsl:template>


<xsl:template name="day">
  <xsl:param name="val"/>
  <xsl:param name="end" select="31"/>
<td align="center">
<xsl:choose>
<xsl:when test="$val > 0 and $end >= $val">
<xsl:value-of select="$val"/>
</xsl:when>
<xsl:otherwise>&#160;</xsl:otherwise>
</xsl:choose>
</td>
</xsl:template>

<xsl:template name="mlength">
<xsl:param name="month"/>
<xsl:param name="leap" select="false()"/>
<xsl:choose>
<xsl:when test="$month= 2 and $leap">29</xsl:when>
<xsl:when test="$month= 2 and not($leap)">28</xsl:when>
<xsl:when test="$month= 4">30</xsl:when>
<xsl:when test="$month= 6">30</xsl:when>
<xsl:when test="$month= 9">30</xsl:when>
<xsl:when test="$month=11">30</xsl:when>
<xsl:otherwise>31</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="moffset">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month= 1">0</xsl:when>
<xsl:when test="$month= 2">3</xsl:when>
<xsl:when test="$month= 3">3</xsl:when>
<xsl:when test="$month= 4">6</xsl:when>
<xsl:when test="$month= 5">1</xsl:when>
<xsl:when test="$month= 6">4</xsl:when>
<xsl:when test="$month= 7">6</xsl:when>
<xsl:when test="$month= 8">2</xsl:when>
<xsl:when test="$month= 9">5</xsl:when>
<xsl:when test="$month=10">0</xsl:when>
<xsl:when test="$month=11">3</xsl:when>
<xsl:when test="$month=12">5</xsl:when>
</xsl:choose>
</xsl:template>


</xsl:stylesheet>


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.