<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- TO DO

1) THE IMAGES NEED A PATH, WHICH I'VE HARDWIRED INTO THIS STYLESHEET; NEED TO FIX THIS?
    MAYBE ADD A VARIABLE TO THE SOURCE XML FILE,
    GIVING IT'S PATH THAT CAN BE PICKED UP BY THIS STYLESHEET?
    -->


	<xsl:output method="xml" omit-xml-declaration="no" />
		<xsl:variable name="colon">:</xsl:variable>
		<xsl:variable name="esccolon">-</xsl:variable>
		<xsl:variable name="localPath" select="resolve-uri('',document-uri(/))" />
		<xsl:strip-space elements="ListItem" />
		
		<xsl:template match="/">
			<rss version="0.91">
				<channel>
				  <title><xsl:value-of select="Item/FrontMatter/CourseCode" /></title> 
				  <link>http://ouseful.open.ac.uk/courseSearch.php</link> 
				  <description>Testbed RSS feed from course search tool.</description>
				  <item>
				  	<title>Acknowledgements</title>
				  	<link>http://labspace.open.ac.uk</link>
				  	<description>The content contained within this document is sourced from
an Open University OpenLearn project labspace unit <xsl:value-of select="Item/FrontMatter/CourseCode" /> - <xsl:value-of select="Item/FrontMatter/CourseTitle" />.</description>
				  </item>
				  <item>
				  	<title>Licensing</title>
				  	<link>http://creativecommons.org/licenses/by-nc-sa/2.0/uk/</link>
				  	<description>This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Licence. </description>
				  </item>
				  <item><title>Overview</title>
					<link>http://openlearn.open.ac.uk</link>
						<description><xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text><xsl:apply-templates select="Item/FrontMatter" /><xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></description>
				  </item>
				  
				  <xsl:apply-templates select="Item/Unit/Session" />
				  <xsl:apply-templates select="Item/Unit/BackMatter" />
				</channel>
			</rss>
		</xsl:template>
	
	<xsl:template match="FrontMatter/Imprint">
	</xsl:template>
	
	<xsl:template match="FrontMatter/Preface">
	</xsl:template>
	
	<xsl:template match="FrontMatter/Introduction">
	<h3>Introduction</h3><xsl:apply-templates /><hr />
	</xsl:template>
	
	<xsl:template match="FrontMatter/LearningOutcomes">
	<h3>Learning Outcomes</h3><xsl:apply-templates /><hr />
	</xsl:template>

	
	<xsl:template match="Unit/BackMatter">
		<item><title>References and Acknowledgements</title>
		<link>http://labspace.open.ac.uk</link>
		<description><xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text><xsl:apply-templates /><xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></description>
		</item>
	</xsl:template>	
	
	<xsl:template match="References">
		<h3>References</h3><xsl:apply-templates />
	</xsl:template>
	
	<xsl:template match="Acknowledgements">
		<h3>Acknowledgements</h3><xsl:apply-templates />
	</xsl:template>
		
	<xsl:template match="Unit/Session">
		<xsl:for-each select="Section[not(SubSection)] | Section/SubSection">
				<xsl:call-template name="feedItem"></xsl:call-template>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="Section">
			<item>
			<title><xsl:value-of select="Title" /></title>
			<link>http://labspace.open.ac.uk</link>
			<description><xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text><xsl:apply-templates /><xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></description>
		</item>
	</xsl:template>
	
		<xsl:template name="feedItem">
			<item>
			<title><xsl:value-of select="Title" /></title>
			<link>http://labspace.open.ac.uk</link>
			<description><xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text><xsl:apply-templates /><xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></description>
		</item>
	</xsl:template>
	
	<xsl:template match="Title">
		<!--<h1><xsl:value-of select="." /></h1>-->
	</xsl:template>

	<xsl:template match="SubSection">
			<item>
			<title><xsl:value-of select="Title" /></title>
			<link>http://labspace.open.ac.uk</link>
			<description><xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text><xsl:apply-templates /><xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></description>
		</item>
	</xsl:template>
	
<!--	
	<xsl:template match="SubSection">
		<div><xsl:apply-templates /></div>
	</xsl:template>
-->
	<xsl:template match="SubSubSection">
		<div><xsl:apply-templates /></div>
	</xsl:template>
	<xsl:template match="Paragraph | LearningOutcome | Reference | Description">
		<p><xsl:apply-templates /></p>
	</xsl:template>
	
	
	<!-- pinched from OU preview xsl, cos I'm lazy.... maybe standard, maybe not... -->
		<xsl:template match="Activity/Timing">
		<p class="paradefault">
			You should allow <xsl:value-of select="Hours"/> hour(s), <xsl:value-of select="Minutes"/> minute(s).
		</p>
	</xsl:template>
	<xsl:template match="Activity | Quote | Verse | Extract | Example">
		<div class="activity">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Answer | Discussion">
	<!--	<a href="javascript: showcontent('{concat(name(),../@id)}')">
			<h3>Now read the <xsl:value-of select="translate(name(),$ucletters,$lcletters)"/></h3>
		</a>
		<div class="activity" id="{concat(name(),../@id)}" style="display:none">
			<xsl:apply-templates/>
		</div> -->
		<hr /><div><xsl:apply-templates /></div><hr />
	</xsl:template>
		<xsl:template match="SourceReference">
		<p class="sourcereference"><xsl:apply-templates/></p>
	</xsl:template>
		<xsl:template match="Heading | SubHeading | Caption">
		<xsl:if test="* or text()"><h3><xsl:apply-templates/></h3></xsl:if>
	</xsl:template>
		<xsl:template match="Table">
		<div align="center">
			<a>
				<xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
				<h3>
					<xsl:value-of select="TableHead"/>
				</h3>
			</a>
			<table class="tableprop" cellpadding="2">
				<xsl:for-each select=".//table/tr">
					<tr>
						<xsl:apply-templates/>
					</tr>
				</xsl:for-each>
			</table>
		</div>
	</xsl:template>
	<xsl:template match="th">
		<td class="tablerowtitle">
			<xsl:attribute name="align"><xsl:choose><xsl:when test="contains(@class,'Cent')">center</xsl:when><xsl:when test="contains(@class,'Right')">right</xsl:when><xsl:otherwise>left</xsl:otherwise></xsl:choose></xsl:attribute>
			<xsl:if test="string-length(@colspan) &gt; 0">
				<xsl:attribute name="colspan"><xsl:value-of select="@colspan"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="string-length(.) = 0">&#160;</xsl:if>
			<xsl:value-of select="."/>
		</td>
	</xsl:template>
	<xsl:template match="td">
		<td class="tablerowvalues">
			<xsl:attribute name="align"><xsl:choose><xsl:when test="contains(@class,'Cent')">center</xsl:when><xsl:when test="contains(@class,'Right')">right</xsl:when><xsl:otherwise>left</xsl:otherwise></xsl:choose></xsl:attribute>
			<xsl:if test="string-length(@colspan) &gt; 0">
				<xsl:attribute name="colspan"><xsl:value-of select="@colspan"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="string-length(.) = 0">&#160;</xsl:if>
			<xsl:value-of select="."/>
		</td>
	</xsl:template>
	<xsl:template match="a">
		<xsl:choose>
			<xsl:when test="@target = '_blank' or contains(@target, 'new')">
				<a target="_blank">
					<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
					<xsl:value-of select="."/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="Figure">
		<div align="center">
			<a>
				<xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
				<xsl:apply-templates select="Image">
					<xsl:with-param name="alt">
						<xsl:value-of select="translate(normalize-space(Caption), $colon, $esccolon)"/>
					</xsl:with-param>
				</xsl:apply-templates>
			</a>
		</div>
	</xsl:template>
	<xsl:template match="Image">
		<xsl:param name="alt"/>
		<xsl:variable name="ImageSrc">
			<xsl:value-of select="@src"/>
		</xsl:variable>
		<img alt="{$alt}" src="{$localPath}{$ImageSrc}">
		</img>
	</xsl:template>
	<xsl:template match="NumberedList | NumberedSubsidiaryList">
		<ol>
			<xsl:for-each select="ListItem | SubListItem">
				<li class="listitem"><xsl:apply-templates/></li>
			</xsl:for-each>
		</ol>
	</xsl:template>
	<xsl:template match="BulletedList | BulletedSubsidiaryList">
		<ul>
			<xsl:for-each select="ListItem | SubListItem">
				<li class="listitem"><xsl:apply-templates/></li>
			</xsl:for-each>
		</ul>
	</xsl:template>
	<xsl:template match="UnNumberedList | UnNumberedSubsidiaryList">
		<dl>
			<xsl:for-each select="ListItem | SubListItem">
				<dd class="listitem"><xsl:apply-templates/></dd>
			</xsl:for-each>
		</dl>
	</xsl:template>

	<!-- media bits -->
			<xsl:template match="MediaContent">
			<xsl:apply-templates select="Caption | Description"/>
				<xsl:choose>
					<xsl:when test="@type = 'audio'">
						<p class="paradefault">
							<a href="{$localPath}{@src}.mp3">Listen in separate player</a>
							<br/>
							<a href="http://ouseful.open.ac.uk/flash_mp3_player/mp3Player.php?curl={$localPath}{@src}">Click to start playing in new window.</a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videoboth'">
						<p class="paradefault">
							<a href="{$localPath}{@src}.mp4" target="_blank">Launch high resolution video</a>
						</p>
						<embed src="http://ouseful.open.ac.uk/flash_flv_player/flvplayer.swf" width="300" height="200" bgcolor="#FFFFFF"
type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" 
flashvars="file={$localPath}{@src}.flv" />
						<p class="paradefault">
							<a href="{$localPath}{@src}.flv">Download low resolution video</a>
							<br/>
							<a href="http://ouseful.open.ac.uk/flash_flv_player/flvPlayer.php?curl={$localPath}{@src}">Click to start playing in new window.</a>
					
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videolow'">
					<embed src="http://ouseful.open.ac.uk/flash_flv_player/flvplayer.swf" width="300" height="200" bgcolor="#FFFFFF"
type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" 
flashvars="file={$localPath}{@src}.flv" />
						<p class="paradefault">
							<a href="{$localPath}{@src}.flv">Download low resolution video</a>
							<br/>
							<a href="http://ouseful.open.ac.uk/flash_flv_player/flvPlayer.php?curl={$localPath}{@src}">Click to start playing in new window.</a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videohigh'">
						<p class="paradefault">
							<a href="{$localPath}{@src}.mp4" target="_blank">Launch high resolution video</a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'flash'">
						<p class="paradefault">
							<a target="new" href="{$localPath}{@src}.swf">Launch in separate player</a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'pdf'">
						<p class="paradefault">
							<a target="_blank" href="{$localPath}{@src}.pdf">View document</a>
						</p>
					</xsl:when>
				</xsl:choose>
				<p class="paradefault">
					<xsl:value-of select="SourceReference"/>
				</p>
				<xsl:if test="@supportDoc != ''">
					<p class="paradefault">
						<a target="_blank" href="{$localPath}{@supportDoc}.pdf">View transcript</a>
					</p>
				</xsl:if>
	</xsl:template>
	<!-- standard bits -->
	
	<xsl:template match="u">
		<u><xsl:apply-templates/></u>
	</xsl:template>
	<xsl:template match="sup">
		<sup><xsl:apply-templates/></sup>
	</xsl:template>
	<xsl:template match="sub">
		<sub><xsl:apply-templates/></sub>
	</xsl:template>
	<xsl:template match="b">
		<strong><xsl:apply-templates /></strong>
	</xsl:template>
		<xsl:template match="i">
		<em><xsl:apply-templates /></em>
	</xsl:template>
	<xsl:template match="text()">
		<xsl:value-of select="." disable-output-escaping="no"/>
	</xsl:template>
	
</xsl:stylesheet>
