<?xml version="1.0" encoding="utf-8"?>
<!-- XSL Transform from Atom 0.3 to XHTML 1.1 -->
<!-- Version 0.2 -->
<!-- Written by Aristotle Pagaltzis -->
<!-- This code is in the public domain -->
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:atom="http://purl.org/atom/ns#"
	xmlns:date="http://exslt.org/dates-and-times"
	xmlns:func="http://exslt.org/functions"
	xmlns:str="http://exslt.org/strings"
	xmlns:my="tag:plasmasturm.org,2004:EXSLT-Functions"
	xmlns="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="atom date func str my"
	extension-element-prefixes="date func str my"
>

<xsl:output
	method="xml"
	indent="no"
	omit-xml-declaration="yes"
	doctype-public="-//W3C//DTD XHTML 1.1//EN"
	doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
	encoding="us-ascii"
/>

<func:function name="my:format-date">
	<xsl:param name="date" />
	<func:result select="concat(
		date:month-abbreviation( $date ),
		' ',
		date:day-in-month( $date ),
		', ',
		date:year( $date )
	)" />
</func:function>

<func:function name="my:format-time">
	<xsl:param name="date" />
	<func:result select="concat(
		str:align( date:hour-in-day( $date ), '00', 'right' ),
		':',
		str:align( date:minute-in-hour( $date ), '00', 'right' )
	)" />
</func:function>

<!-- define a few utility templates first of all -->

<xsl:template match="node()" mode="copy-checking-escaping">
	<xsl:value-of select="normalize-space(.)" />
</xsl:template>

<xsl:template match="node()[@mode='xml']" mode="copy-checking-escaping">
	<xsl:copy-of select="*" />
</xsl:template>

<xsl:template match="node()[@mode='escaped']" mode="copy-checking-escaping">
	<xsl:value-of select="normalize-space(.)" disable-output-escaping="yes" />
</xsl:template>

<xsl:template match="node()" mode="make-link">
	<xsl:variable name="href" select="../atom:link[@rel='alternate' and @type='text/html']/@href" />
	<xsl:choose>
		<xsl:when test="$href">
			<a href="{$href}"><xsl:apply-templates select="." mode="copy-checking-escaping" /></a>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="." mode="copy-checking-escaping" />
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- now we put the style in “stylesheet” -->

<!-- discard by default: -->
<xsl:template match="node()|@*" mode="atom-feed" />

<xsl:template match="atom:entry/atom:issued|atom:entry/atom:modified" mode="atom-feed">
	<div class="{ local-name( . ) }">
		<xsl:text>(</xsl:text>
		<span class="date"><xsl:value-of select="my:format-date( . )" /></span>
		<xsl:text> @ </xsl:text>
		<span class="time"><xsl:value-of select="my:format-time( . )" /></span>
		<xsl:text>)</xsl:text>
	</div>
</xsl:template>

<xsl:template match="atom:tagline|atom:entry/atom:summary|atom:entry/atom:content" mode="atom-feed">
	<div class="{ local-name( . ) }">
		<xsl:apply-templates select="." mode="copy-checking-escaping" />
	</div>
</xsl:template>

<xsl:template match="atom:feed/atom:entry" mode="atom-feed">
	<div class="entry">
		<h2><xsl:apply-templates select="atom:title" mode="make-link" /></h2>
		<xsl:apply-templates select="atom:issued" mode="atom-feed" />
		<xsl:if test="date:seconds( date:difference( atom:issued, atom:modified ) ) != 0">
			<xsl:apply-templates select="atom:modified" mode="atom-feed" />
		</xsl:if>
		<xsl:apply-templates select="atom:summary" mode="atom-feed" />
		<xsl:apply-templates select="atom:content" mode="atom-feed" />
	</div>
</xsl:template>

<xsl:template match="atom:feed[@version = '0.3']" mode="atom-feed">
	<div class="feed">
		<h1><xsl:apply-templates select="atom:title" mode="make-link" /></h1>
		<xsl:apply-templates select="atom:tagline" mode="atom-feed" />
		<xsl:apply-templates select="atom:entry" mode="atom-feed">
			<xsl:sort select="date:seconds( atom:issued )" data-type="number" order="descending" />
		</xsl:apply-templates>
	</div>
</xsl:template>

<xsl:template match="/">
	<html>
		<head>
			<title><xsl:value-of select="normalize-space( atom:feed/atom:title )" /></title>
		</head>
		<body>
			<xsl:apply-templates select="atom:feed" mode="atom-feed" />
		</body>
	</html>
</xsl:template>

</xsl:stylesheet>
