Newer
Older
XML / html_calendar.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<xsl:output method="html" encoding="ISO-8859-1" media-type="text/html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>

	<xsl:param name="standalone">yes</xsl:param>

	<xsl:strip-space elements="*"/>

	<xsl:variable name="rows-per-week">
		<xsl:value-of select="/calendar/@rows-per-week"/>
		<xsl:if test="not(/calendar/@rows-per-week)">2</xsl:if>
	</xsl:variable>

	<xsl:variable name="number-of-weeks">
		<xsl:value-of select="/calendar/@number-of-weeks"/>
		<xsl:if test="not(/calendar/@number-of-weeks)">13</xsl:if>
	</xsl:variable>
	
	<!--
		This is the number of columns in the output table.
	 -->
	<xsl:variable name="number-of-columns">11</xsl:variable>

	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$standalone = 'yes'">
				<html>
					<head>
						<title>
							<xsl:apply-templates select="calendar/paper"/>
							<xsl:text> Course Calendar, </xsl:text>
							<xsl:apply-templates select="calendar/period"/>
							<xsl:text> </xsl:text>
							<xsl:apply-templates select="calendar/year"/>
						</title>
						<link rel="Stylesheet" href="http://info-nts-05.otago.ac.nz/info321/db_styles.css" type="text/css"/>
					</head>
					<body>
						<xsl:apply-templates/>
					</body>
				</html>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="calendar">
		<!-- Page heading. -->
		<table border="0" cellspacing="0" cellpadding="4" width="99%">
			<tr><td class="blue-bb-left">
				<font face="arial,helvetica" size="3"><strong>
					<xsl:apply-templates select="paper"/>
					<xsl:text> Course Calendar, </xsl:text>
					<xsl:apply-templates select="period"/>
					<xsl:text> </xsl:text>
					<xsl:apply-templates select="year"/>
				</strong></font>
			</td></tr>
			<tr><td>
				<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
			</td></tr>
		</table>
		
		<!-- The calendar itself. -->
		<center>
			<table border="1" summary="Course Calendar with relevant links" cellspacing="0">
				<!--
					I was going to generate COL elements here, but they
					don't seem to be supported by all browsers. Damn. The same
					applies for THEAD/TFOOT/TBODY, but I may as well leave them in
					for the browsers that do support them.
				-->
				<thead>
					<xsl:apply-templates select="header"/>
				</thead>
				<tfoot>
					<xsl:apply-templates select="footer"/>
				</tfoot>
				<tbody>
					<xsl:apply-templates select="body"/>
				</tbody>
			</table>
		</center>
	</xsl:template>
	
	<!--
		Header/footer rows for the calendar table. To get proper table
		headings/footings, use HEADING/FOOTING. For other text, use NOTE.
	-->
	<xsl:template match="header|footer">
		<tr>
			<xsl:apply-templates/>
		</tr>
	</xsl:template>
	
	<!--
		A table heading or footing, coloured appropriately, bolded,
		centered, etc.
	-->
	<xsl:template match="heading|footing">
		<th>
			<xsl:if test="@columns">
				<xsl:attribute name="colspan">
					<xsl:value-of select="@columns"/>
				</xsl:attribute>
			</xsl:if>
			<div class="small"><xsl:apply-templates/></div>
		</th>
	</xsl:template>
	
	<!--
		A miscellaneous piece of text to be inserted somewhere in the
		calendar table.
	-->
	<xsl:template match="note">
		<td>
			<xsl:if test="@columns">
				<xsl:attribute name="colspan">
					<xsl:value-of select="@columns"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
		</td>
	</xsl:template>
	
	<!--
		A week in the calendar. Each week has an associated number
		(automatically generated) and a date range, and spans some
		defined number of rows (default 2). The week number and
		date range are only generated for the first of these rows.
	-->
	<xsl:template match="week">
		<xsl:for-each select="row">
			<tr>
				<!-- Output the week number and dates columns. -->
				<xsl:if test="position() = 1">
					<!-- Week number, first row only. -->
					<td class="white-center">
						<xsl:attribute name="rowspan">
							<xsl:value-of select="$rows-per-week"/>
						</xsl:attribute>
						<div class="small">
							<xsl:number value="1 + count(preceding::week[not(@holiday)])"/>
						</div>
					</td>
	
					<!-- Date range, first row only. -->
					<td class="white-center">
						<xsl:attribute name="rowspan">
							<xsl:value-of select="$rows-per-week"/>
						</xsl:attribute>
						<div class="small">
							<xsl:apply-templates select="../dates"/>
						</div>
					</td>
				</xsl:if>

				<!--
					Apply each of the sub-templates in the correct order.
					Missing elements don't matter.
				-->
				<xsl:apply-templates select="section"/>
				<xsl:apply-templates select="lecture"/>
				<xsl:apply-templates select="reading"/>
				<xsl:apply-templates select="laboratory"/>
				<xsl:apply-templates select="tutorial"/>
				<xsl:apply-templates select="assessment"/>
			</tr>
		</xsl:for-each>
	</xsl:template>
	
	<!--
		Weeks that are holidays have no week number or date range, just
		a text description.
	-->
	<xsl:template match="week[@holiday]">
		<tr>
			<td class="blue-ou-center">
				<xsl:attribute name="colspan">
					<xsl:value-of select="$number-of-columns"/>
				</xsl:attribute>
				<strong class="large"><em><xsl:apply-templates/></em></strong>
			</td>
		</tr>
	</xsl:template>
	
	<!--
		Output the date range for a week. We need a separate template
		because it's likely to have embedded ENDASH elements.
	-->
	<xsl:template match="dates">
		<div class="small"><xsl:apply-templates/></div>
	</xsl:template>
	
	<!--
		Output a calendar entry for a lecture. Note that lectures are
		always assumed to span a single row. Lectures are automatically
		numbered.
	-->
	<xsl:template match="lecture[node() and not(@holiday)]">
		<td class="ltgrey-center">
			<div class="small">
				<xsl:number value="1 + count(preceding::lecture[node()])"/>
			</div>
		</td>
		<td class="ltgrey-left" rowspan="1">
			<div class="small"><xsl:apply-templates/></div>
		</td>
	</xsl:template>
	
	<!--
		Output a calendar entry for a lecture that occurs on a holiday.
		These aren't numbered.
	-->
	<xsl:template match="lecture[@holiday]">
		<td class="blue-ou-center" colspan="2">
			<strong class="small"><em><xsl:apply-templates/></em></strong>
		</td>
	</xsl:template>
	
	<!--
		Output an empty lecture cell.
	-->
	<xsl:template match="lecture[not(node())]">
		<td class="medgrey-center" colspan="2">
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</xsl:template>
	
	<!--
		Output a calendar entry for a laboratory. Laboratories are
		automatically numbered.
	-->
	<xsl:template match="laboratory[node()]">
		<xsl:variable name="num-rows">
			<xsl:value-of select="@rows"/>
			<xsl:if test="not(@rows)">
				<xsl:value-of select="$rows-per-week"/>
			</xsl:if>
		</xsl:variable>
		<td class="ltblue-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="$num-rows"/>
			</xsl:attribute>
			<div class="small">
				<xsl:number value="1 + count(preceding::laboratory[node()])"/>
			</div>
		</td>
		<td class="ltblue-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="$num-rows"/>
			</xsl:attribute>
			<div class="small"><xsl:apply-templates/></div>
		</td>
	</xsl:template>
	
	<!--
		Output an empty laboratory cell.
	-->
	<xsl:template match="laboratory[not(node())]">
		<td class="medgrey-center" colspan="2">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</xsl:template>
	
	<!--
		Output a calendar entry for a tutorial. Tutorials are
		automatically numbered.
	-->
	<xsl:template match="tutorial[node()]">
		<xsl:variable name="num-rows">
			<xsl:value-of select="@rows"/>
			<xsl:if test="not(@rows)">
				<xsl:value-of select="$rows-per-week"/>
			</xsl:if>
		</xsl:variable>
		<td class="medgreen-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="$num-rows"/>
			</xsl:attribute>
			<div class="small">
				<xsl:number value="1 + count(preceding::tutorial[node()])"/>
			</div>
		</td>
		<td class="medgreen-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="$num-rows"/>
			</xsl:attribute>
			<div class="small"><xsl:apply-templates/></div>
		</td>
	</xsl:template>
	
	<!--
		Output an empty tutorial cell.
	-->
	<xsl:template match="tutorial[not(node())]">
		<td class="medgrey-center" colspan="2">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</xsl:template>
	
	<!--
		Output a calendar entry for a section or reading (both use the same
		formatting).
	-->
	<xsl:template match="section[node()]|reading[node()]">
		<td class="white-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<div class="small"><xsl:apply-templates/></div>
		</td>
	</xsl:template>
	
	<!--
		Output an empty section or reading cell.
	-->
	<xsl:template match="section[not(node())]|reading[not(node())]">
		<td class="medgrey-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</xsl:template>
	
	<!--
		Output a calendar entry for an assessment.
	-->
	<xsl:template match="assessment[node()]">
		<td class="blue-bb-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<div class="small"><xsl:apply-templates/></div>
		</td>
	</xsl:template>
	
	<!--
		Output an empty assessment cell.
	-->
	<xsl:template match="assessment[not(node())]">
		<td class="medgrey-center">
			<xsl:attribute name="rowspan">
				<xsl:value-of select="@rows"/>
				<xsl:if test="not(@rows)">
					<xsl:value-of select="$rows-per-week"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</xsl:template>
	
	
	<!--
		Generate a link.
	-->
	<xsl:template match="link">
		<a>
			<xsl:attribute name="href">
				<xsl:value-of select="@href"/>
			</xsl:attribute>
			<xsl:apply-templates/>
		</a>
	</xsl:template>
	
	
	<!--
		Wrap quotes around a string. If the attribute SINGLE is set to
		"yes", use single quotes instead of double. Use numeric entity
		references because not all browsers support &ldquo;, &rdquo;,
		&lsquo; and &rsquo;.
	-->
	<xsl:template match="quote">
		<xsl:choose>
			<xsl:when test="@single = 'yes'">
				<xsl:text disable-output-escaping="yes">&amp;#8216;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text disable-output-escaping="yes">&amp;#8220;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:apply-templates/>
		<xsl:choose>
			<xsl:when test="@single = 'yes'">
				<xsl:text disable-output-escaping="yes">&amp;#8217;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text disable-output-escaping="yes">&amp;#8221;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!--
		Output an ampersand entity (&amp;).
	-->
	<xsl:template match="ampersand">
		<xsl:text disable-output-escaping="yes">&amp;amp;</xsl:text>
	</xsl:template>
	
	<!--
		Output an en-dash entity (&#8211; because not all browsers support
		&ndash;).
	-->
	<xsl:template match="endash">
		<xsl:text disable-output-escaping="yes">&amp;#8211;</xsl:text>
	</xsl:template>
	
	<!--
		Output a section symbol entity (&#167; because not all browsers support
		&sect;).
	-->
	<xsl:template match="sect">
		<xsl:text disable-output-escaping="yes">&amp;#167;</xsl:text>
	</xsl:template>
	
	<!--
		Output a hash character.
	-->
	<xsl:template match="hash">
		<xsl:text>#</xsl:text>
	</xsl:template>
	
	<!--
		Miscellaneous HTML elements to be mapped straight through.
	-->
	<xsl:template match="strong">
		<strong><xsl:apply-templates/></strong>
	</xsl:template>
	
	<xsl:template match="em">
		<em><xsl:apply-templates/></em>
	</xsl:template>
	
	<xsl:template match="b">
		<b><xsl:apply-templates/></b>
	</xsl:template>
	
	<xsl:template match="i">
		<i><xsl:apply-templates/></i>
	</xsl:template>
	
	<xsl:template match="br">
		<br/>
	</xsl:template>
	
</xsl:stylesheet>