<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
		xmlns:dc="http://purl.org/dc/terms/"
		xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#"
		xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
   		xmlns="http://purl.org/rss/1.0/"
		exclude-result-prefixes="xhtml"
                version="1.0">

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

<xsl:output method="xml" 
		   indent="yes" 
		   encoding="UTF-8" 
		   media-type="application/xml"/>

<!-- hAudio 2 RSS RDF/GRDDL Profile *Stable* by Martin McEvoy $version 5.0 updated Saturday, Saturday, November 22 2008 16:27 +0100 $contact infoattribute::weborganics.co.uk-->

<!-- base of the current HTML doc set by Processor-->
<xsl:param name="base-uri" select="''"/>

<xsl:param name="start" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' haudio ') and descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' item ')]]"/>

<xsl:param name="index">
<xsl:choose>
     <xsl:when test="descendant::xhtml:a/attribute::href[1]">
		<xsl:value-of select="descendant::xhtml:a/attribute::href[1]" />
	</xsl:when>
    <xsl:otherwise>
		<xsl:value-of select="$base-uri" />
    </xsl:otherwise>
</xsl:choose>
</xsl:param>

<xsl:param name="items" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' item ') and descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' fn ')]]"/>

<xsl:param name="audio" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' haudio ') and descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' fn ')]]"/>

<xsl:template match="/">
<rdf:RDF>
    <xsl:apply-templates/>
<xsl:choose>
     	<xsl:when test="$items">
		<xsl:call-template name="item"/>
	</xsl:when>
    	<xsl:otherwise>
   		<xsl:call-template name="haudio"/>
    	</xsl:otherwise>
</xsl:choose>
</rdf:RDF>
</xsl:template>

<!-- ==============================================Temaplates===============================================-->

<xsl:template match="xhtml:html">
<xsl:param name="description" select="descendant::*[contains(concat(' ',normalize-space(attribute::name),' '),' description ')]"/>
<xsl:param name="published" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' published ')]"/>
<xsl:param name="title" select="descendant::xhtml:*[contains(concat(' ',normalize-space(@class),' '),' fn ') and not(ancestor::*[contains(concat(' ',normalize-space(attribute::class),' '),' vcard ')])]"/>
<xsl:param name="photo" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' logo ')]|descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' photo ')]"/>
<channel>
	<xsl:attribute name='rdf:about'>
		<xsl:value-of select="$index"/>
	</xsl:attribute>
<xsl:choose>
     <xsl:when test="$start">
		<title><xsl:value-of select="$title"/></title>
	</xsl:when>
    <xsl:otherwise>
		<title><xsl:value-of select="//*[name() = 'title']"/></title>
    </xsl:otherwise>
</xsl:choose>
<link><xsl:value-of select="$index"/></link>
<xsl:if test="$published">
	<dc:date><xsl:value-of select="$published[1]" /></dc:date>
</xsl:if>

<xsl:choose>
<xsl:when test="$items">
	<xsl:if test="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' description ')]">
		<description>
			<xsl:value-of select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' description ')]" />
		</description>
	</xsl:if>
</xsl:when>
<xsl:otherwise>
	<xsl:if test="$description">
		<description>
			<xsl:value-of select="$description/attribute::content" />
		</description>
	</xsl:if>
</xsl:otherwise>
</xsl:choose>

<items>     
	<rdf:Seq>
		<xsl:choose>
     			<xsl:when test="$items">
				<xsl:call-template name="items"/>
			</xsl:when>
    			<xsl:otherwise>
				<xsl:call-template name="haudios"/>
    			</xsl:otherwise>
		</xsl:choose>
	</rdf:Seq>
</items>
</channel>
</xsl:template>

<xsl:template name="items">
	<xsl:for-each select="$items">
        	<rdf:li rdf:nodeID="{generate-id()}"/>
	</xsl:for-each>
</xsl:template>

<xsl:template name="haudios">
	<xsl:for-each select="$audio">
        	<rdf:li rdf:nodeID="{generate-id()}"/>
	</xsl:for-each>
</xsl:template>

<xsl:template name="haudio">
  	<xsl:for-each select="$audio">
			<xsl:call-template name="item-content"/>
	</xsl:for-each>
</xsl:template>

<xsl:template name="item">
  	<xsl:for-each select="$items">
			<xsl:call-template name="item-content"/>
	</xsl:for-each>
</xsl:template>

<xsl:template name="item-content">
	<xsl:element name='item'>
  		<xsl:attribute name='rdf:nodeID'>
			<xsl:value-of select="generate-id()"/>
		</xsl:attribute>
		<xsl:call-template name="title"/>
			<xsl:call-template name="link"/>
			<xsl:call-template name="pubDate"/>
			<xsl:call-template name="author"/>
			<xsl:call-template name="itemDescription"/>
			<xsl:call-template name="enclosure"/>
	</xsl:element>
</xsl:template>

<xsl:template name="title">
<xsl:param name="title" select="descendant::xhtml:*[contains(concat(' ',normalize-space(@class),' '),' fn ') and not(ancestor::*[contains(concat(' ',normalize-space(attribute::class),' '),' vcard ')])]"/>
	<xsl:if test="$title">
		<xsl:element name='title'>
			<xsl:value-of select="$title"/>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template name="link">
<xsl:param name="href" select="descendant::*[name() = 'a']/attribute::href"/>
<xsl:param name="titlehref" select="descendant::xhtml:*[contains(concat(' ',normalize-space(@class),' '),' fn ') and not(ancestor::*[contains(concat(' ',normalize-space(attribute::class),' '),' vcard ')])]/@href"/>
<xsl:choose>
	<xsl:when test="$titlehref">
		<xsl:element name='link'>
			<xsl:value-of select="$titlehref"/>
		</xsl:element>
	</xsl:when>
	<xsl:otherwise>
	<xsl:if test="$href">
		<xsl:element name='link'>
			<xsl:value-of select="$href"/>
		</xsl:element>
	</xsl:if>
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="pubDate">
<xsl:param name="date" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' published ')]"/>
<xsl:param name="xsdDate" select="descendant::*[contains(concat(' ',normalize-space(@datatype),' '),' xsd:dateTime ')]"/>
<xsl:choose>
    <xsl:when test="$date">
		<xsl:element name='date'>
			<xsl:value-of select="$date/@title"/>
		</xsl:element>
	</xsl:when>
    <xsl:otherwise>
		<xsl:if test="$xsdDate">
			<xsl:element name='date'>
				<xsl:value-of select="$xsdDate/@content"/>
			</xsl:element>
		</xsl:if>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="author">
<xsl:param name="name" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' contributor ')]"/>
<xsl:param name="fallback" select="/*//*[contains(concat(' ',normalize-space(attribute::class),' '),' contributor ')][1]"/>
<xsl:choose>
     <xsl:when test="$name">
		<xsl:element name='dc:creator'>
			<xsl:value-of select="$name"/>
		</xsl:element>
	</xsl:when>
    	<xsl:otherwise>
	<xsl:if test="$fallback">
		<xsl:element name='dc:creator'>
			<xsl:value-of select="$fallback"/>
		</xsl:element>
	</xsl:if>
    	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="itemDescription">
<xsl:param name="itemDesc" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' description ')]"/>
<xsl:param name="img" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' photo ') and not(ancestor::*[contains(concat(' ',normalize-space(attribute::class),' '),' description ')])]"/>
	<xsl:if test="$itemDesc|$img">
		<xsl:element name='description'>
  			<xsl:attribute name='rdf:datatype'>
		     		<xsl:text>http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral</xsl:text>
			</xsl:attribute>
		     <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
				<xsl:copy-of select="$img"/>
				<xsl:copy-of select="$itemDesc"/>
		     <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template name="enclosure">
<xsl:param name="enc" select="descendant::*[contains(concat(' ',normalize-space(attribute::rel),' '),' enclosure ')]"/>
<xsl:param name="length" select="descendant::*[contains(concat(' ',normalize-space(attribute::class),' '),' length ')]"/>
<xsl:if test="$enc">
	<xsl:element name='enc:enclosure'>
		<xsl:element name='rdf:Description'>
  			<xsl:attribute name='rdf:about'>
				<xsl:value-of select="$enc/attribute::href"/>
			</xsl:attribute>
  			<xsl:element name='enc:url'>
				<xsl:value-of select="$enc/attribute::href"/>
			</xsl:element>
  			<xsl:element name='enc:type'>
				<xsl:value-of select="$enc/attribute::type"/>
			</xsl:element>
			<xsl:if test="$length">
  				<xsl:element name='enc:length'>
					<xsl:value-of select="$length/attribute::title"/>
				</xsl:element>
			</xsl:if>
		</xsl:element>
	</xsl:element>
</xsl:if>
</xsl:template>

<!-- strip text -->
<template match="text()|attribute::*|*">
  <apply-templates/>
</template>

</xsl:stylesheet>
