<?xml version="1.0" encoding="UTF-8"?>
<stylesheet
    xmlns  ="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsl  ="http://www.w3.org/1999/XSL/Transform"
    xmlns:h    ="http://www.w3.org/1999/xhtml"
    xmlns:rdf  ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:v  ="http://www.w3.org/2006/vcard/ns#">


<!-- hGRDDL hCalendar : GRDDL transformation from hCalendar to microformats
	 Version 0.1 by Fabien.Gandon@sophia.inria.fr                   -->


<output indent="yes" method="xml" media-type="text/html" encoding="UTF-8" omit-xml-declaration="yes"/>

<!-- templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <template match="/">
   <copy>
  	  <apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
  </template>  

  <!-- declare namespaces -->
  <template match="h:html">
  	  <html xmlns="http://www.w3.org/1999/xhtml"
  	  	 xmlns:rdf  ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  	  	 xmlns:c  ="http://www.w3.org/2002/12/cal#">
        <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
      </html>
  </template>  

 <!-- update transform -->
 <template match="h:link[attribute::rel='transformation' and attribute::href='hCalendar2RDFa.xsl']">
   <link rel="transformation" href="RDFa2RDFXML.xsl" xmlns="http://www.w3.org/1999/xhtml"/>
 </template>
 
 
 <!-- Vevent templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <!-- c:Vevent -->
 <template match="*[attribute::class='vevent']">
   <copy>
    <apply-templates select="@*" /> 
    <if test="not(attribute::about) and attribute::id"><attribute name="about">#<value-of select="attribute::id"/></attribute></if>
    <if test="not(attribute::about) and not(attribute::id)"><attribute name="about">#<value-of select="generate-id()"/></attribute></if>
    <apply-templates select="text()[string-length(normalize-space(.))=0][position()=1]" /> 
    <link xmlns="http://www.w3.org/1999/xhtml" rel="rdf:type" href="c:Vevent"/>
   	<apply-templates select="*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>

 <!-- v:photo -->
 <template match="h:img[attribute::class='photo']">
   <copy><xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /></copy>
   <apply-templates select="../text()[string-length(normalize-space(.))=0][position()=1]" /> 
   <span xmlns="http://www.w3.org/1999/xhtml" rel="v:photo" href="{@src}"/>
 </template>
 
 <!-- a -->
 <template match="h:a">
   <copy>
   	<if test="ancestor::*[attribute::class='vevent'] and (contains(attribute::class,' url ') or starts-with(attribute::class,'url ') or attribute::class='url')">
   		<attribute name="rel"><value-of select="normalize-space(concat('c:url ',attribute::rel))"/></attribute>
   	</if>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- c:dtstart -->
 <template match="*[attribute::class='dtstart' and not(attribute::title) and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:dtstart ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- c:dtend -->
 <template match="*[attribute::class='dtend' and not(attribute::title) and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:dtend ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>

  <!-- c:dtstart -->
 <template match="*[attribute::class='dtstart' and not(attribute::content) and attribute::title and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:dtstart ',attribute::property))"/></attribute>
    <attribute name="content"><value-of select="attribute::title"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- c:dtend -->
 <template match="*[attribute::class='dtend' and not(attribute::content) and attribute::title and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:dtend ',attribute::property))"/></attribute>
    <attribute name="content"><value-of select="attribute::title"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>

  <!-- c:summary -->
 <template match="*[attribute::class='summary' and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:summary ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 

  <!-- c:location -->
 <template match="*[attribute::class='location' and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:location ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
 
 
 <!-- c:description -->
 <template match="*[attribute::class='description' and ancestor::*/attribute::class='vevent']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('c:description ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
  
 <!-- copy everything -->
 <template match="@*|*|processing-instruction()|comment()">
  <copy><apply-templates select="*|@*|text()|processing-instruction()|comment()" /></copy>
 </template>

</stylesheet>
