<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:h="http://www.w3.org/1999/xhtml"
  xmlns:i="http://www.w3.org/2002/12/cal/icaltzd#"
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  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:dcterms="http://purl.org/dc/terms/">
   <rdf:Description rdf:about="">
    <dc:description>This stylesheet is designed to extract historical event metadata from XHTML and generate RDFical.
   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>2007-10-07</dcterms:modified>
    <dc:rights>(c) 2004-2007 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">
  <xsl:choose>
   <xsl:when test="/h:html/@xml:lang"><xsl:value-of select="/h:html/@xml:lang"/></xsl:when>
   <xsl:when test="/h:html/@lang"><xsl:value-of select="/h:html/@lang"/></xsl:when>
  </xsl:choose>
 </xsl:variable>

 <xsl:template match="/">
  <xsl:apply-templates select="h:html"/>
 </xsl:template>

 <xsl:template match="h:html">
  <rdf:RDF xml:lang="{$lang}">
   <i:Vcalendar rdf:ID="calendar">
    <rdfs:label>RDF Calendar version of the history</rdfs:label>
    <i:prodid>-//kanzaki.com//RDFCal 1.0//EN</i:prodid>
    <i:version>2.0</i:version>
    <i:method>PUBLISH</i:method>
    <x:wrCalname><xsl:value-of select="h:head/h:title"/></x:wrCalname>
    <xsl:apply-templates select="h:body//*[@class='i.event']"/>
   </i:Vcalendar>
  </rdf:RDF>
  <rdf:Description rdf:about="">
   <foaf:topic rdf:resource="#calendar"/>
  </rdf:Description>
 </xsl:template>

 <xsl:template match="*[@class='i.event']">
  <xsl:variable name="myid" select="@id"/>
  <i:component>
   <i:Vevent rdf:nodeID="{$myid}">
    <rdfs:label><xsl:value-of select="."/></rdfs:label>
    <i:summary><xsl:value-of select="."/></i:summary>
    <xsl:call-template name="finditems">
     <xsl:with-param name="elts" select="ancestor::*[@class='i.period']//*[contains(@class,concat('ref.',$myid))]"/>
    </xsl:call-template>
    <xsl:call-template name="findrefs">
     <xsl:with-param name="refs" select="ancestor::*[@class='i.period']//h:a[@rel=concat('ref.',$myid)]"/>
    </xsl:call-template>
   </i:Vevent>
  </i:component>
 </xsl:template>

 <xsl:template name="finditems">
  <xsl:param name="elts"/>
  <xsl:for-each select="$elts">
   <xsl:call-template name="eventitem">
    <xsl:with-param name="prop" select="substring-after(@class,' ')"/>
   </xsl:call-template>
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="eventitem">
  <xsl:param name="prop"/>
  <xsl:element name="{translate($prop,'.',':')}">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template name="findrefs">
  <xsl:param name="refs"/>
  <xsl:for-each select="$refs">
   <i:homepage rdf:resource="{@href}" rdfs:label="{.}"/>
  </xsl:for-each>
 </xsl:template>


</xsl:stylesheet>
