<xsl:stylesheet version="1.0"
  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="http://www.heml.org/schemas/2002-05-29/heml"
  xmlns:i="http://www.w3.org/2002/12/cal/ical#"
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:x="http://www.w3.org/2002/12/cal/prod/kanzaki.co_aecc9ad85690eae3#"
>

 <xsl:template name="notice">
  <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:dcterms="http://purl.org/dc/terms/">
   <rdf:Description rdf:about="">
    <dc:description>This stylesheet is designed to convert RDFical to HEML.
   You can redistribute it and/or modify it under the terms of 
   the GPL (GNU General Public License) .</dc:description>
    <foaf:maker>
     <foaf:Person rdf:about="urn:pin:MK705" foaf:name="Masahide Kanzaki">
      <foaf:mbox rdf:resource="mailto:webmaster@kanzaki.com"/>
     </foaf:Person>
    </foaf:maker>
    <dcterms:created>2004-03-23</dcterms:created>
    <dcterms:modified>2004-03-24</dcterms:modified>
    <dc:rights>(c) 2004 by Masahide Kanzaki, copyleft under GPL</dc:rights>
    <cc:license rdf:resource="http://creativecommons.org/licenses/GPL/2.0/"/>
   </rdf:Description>
  </rdf:RDF>
 </xsl:template>

 <xsl:output method="xml" indent="yes"/>
 <xsl:param name="xmlfile"/>

 <xsl:variable name="lang" select="/rdf:RDF/@xml:lang"/>

 <xsl:template match="/">
  <xsl:apply-templates select="rdf:RDF/i:Vcalendar"/>
 </xsl:template>

 <xsl:template match="i:Vcalendar">
  <Heml>
   <Head>
    <dc:title><xsl:value-of select="x:wrCalname"/></dc:title>
   </Head>
   <Events>
    <xsl:apply-templates select="i:component/i:Vevent"/>
   </Events>
  </Heml>
 </xsl:template>

 <xsl:template match="i:Vevent">
  <Event id="{@rdf:nodeID}">
   <EventLabelSet><Label xml:lang="{$lang}"><xsl:value-of select="i:summary"/></Label></EventLabelSet>
   <xsl:if test="i:dtstart">
    <Chronology>
     <xsl:apply-templates select="i:description"/>
     <xsl:choose>
      <xsl:when test="i:dtend">
       <DateRange>
        <xsl:apply-templates select="i:dtstart"/>
        <xsl:apply-templates select="i:dtend"/>
       </DateRange>
      </xsl:when>
      <xsl:otherwise>
       <xsl:call-template name="yearDate">
        <xsl:with-param name="dval" select="i:dtstart/i:date|i:dtstart/i:dateTime"/>
       </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </Chronology>
   </xsl:if>
   <xsl:apply-templates select="dc:creator|dc:contributor"/>
   <xsl:if test="i:homepage">
    <References>
     <Evidences>
      <xsl:apply-templates select="i:homepage"/>
     </Evidences>
    </References>
   </xsl:if>
  </Event>
 </xsl:template>

 <xsl:template match="i:dtstart">
  <StartingDate>
   <xsl:call-template name="yearDate">
    <xsl:with-param name="dval" select="./i:date|./i:dateTime"/>
   </xsl:call-template>
  </StartingDate>
 </xsl:template>

 <xsl:template match="i:dtend">
  <EndingDate>
   <xsl:call-template name="yearDate">
    <xsl:with-param name="dval" select="./i:date|./i:dateTime"/>
   </xsl:call-template>
  </EndingDate>
 </xsl:template>

 <xsl:template name="yearDate">
  <xsl:param name="dval"/>
  <xsl:choose>
   <xsl:when test="string-length($dval) &lt; 5">
    <XMLSchemaGYear><xsl:value-of select="$dval"/></XMLSchemaGYear>
   </xsl:when>
   <xsl:when test="contains($dval,'T')">
    <XMLSchemaDate><xsl:value-of select="substring-before($dval,'T')"/></XMLSchemaDate>
   </xsl:when>
   <xsl:otherwise>
    <XMLSchemaDate><xsl:value-of select="$dval"/></XMLSchemaDate>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="i:description">
  <Comment><xsl:value-of select="."/></Comment>
 </xsl:template>

 <xsl:template match="i:homepage">
  <Evidence>
   <NetworkedSourceSet>
    <SimpleLink xml:lang="{$lang}" xlink:href="{@rdf:resource}"><xsl:value-of select="@rdfs:label"/></SimpleLink>
   </NetworkedSourceSet>
  </Evidence>
 </xsl:template>

 <xsl:template match="dc:creator|dc:contributor">
  <Participants>
   <PersonWithRole>
    <Person id="{generate-id()}">
     <NameSet><Name xml:lang="{$lang}">
       <NameElement ordinal="1"><xsl:value-of select="."/></NameElement>
      </Name></NameSet>
    </Person>
    <RoleLabelSet>
     <Label xml:lang="{$lang}"><xsl:value-of select="local-name()"/></Label>
    </RoleLabelSet>
   </PersonWithRole>
  </Participants>
 </xsl:template>

</xsl:stylesheet>
