<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="http://www.w3.org/1999/xhtml"
  xmlns:i="http://www.w3.org/2002/12/cal/ical#"
  xmlns:p="http://www.usefulinc.com/picdiary/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:rss="http://purl.org/rss/1.0/"
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
  xmlns:rs="http://purl.org/net/rdf/papers/sitemap#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  exclude-result-prefixes="rdf i dc geo rss rs"
>

 <xsl:template name="notice">
 <!-- an explanatory message-->
  <div class="lead" style="margin-left:3em;margin-right:2em">
   <h2>Welcome and enjoy</h2>
   <p>This stylesheet is designed to convert RDFical file to visible XHTML with manthly calendar. This also handles RSS file with RDFical vocabulary.
    This version (2004-12-30) has been edited by Libby Miller to display images.
   </p>
  <rdf:RDF
   xmlns:foaf="http://xmlns.com/foaf/0.1/"
   xmlns:cc="http://web.resource.org/cc/">
   <rdf:Description rdf:about="">
    <dc:description>This stylesheet is copyright (c) 2003-2004 by Masahide Kanzaki. 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>
    <dc:rights>(c) 2003-2004 by the author, copyleft under GPL</dc:rights>
    <cc:license rdf:resource="http://creativecommons.org/licenses/GPL/2.0/"/>
   </rdf:Description>
  </rdf:RDF>
  </div>
 </xsl:template>

 <xsl:output method="html"/>
 <xsl:variable name="mkcateg"><xsl:value-of select="/rdf:RDF/i:Vcalendar/*[local-name()='mkCateg']"/></xsl:variable>
 <xsl:variable name="calname"><xsl:value-of select="/rdf:RDF/i:Vcalendar/*[local-name()='wrCalname']|/rdf:RDF/rss:channel/rss:title"/></xsl:variable>

 <!-- title, h1 and footer comes here to be displayed if this XSLT directly called from UA -->
 <xsl:template match="/">
  <html>
   <head>
    <title>RDFical view: <xsl:value-of select="$calname"/></title>
    <xsl:call-template name="htmlhead"/>
   </head>
   <body onload="newcal('{rdf:RDF/@xml:lang}');">
    <h4><xsl:value-of select="$calname"/><xsl:if test="not($calname)">RDFical XSLT rendering</xsl:if>
     Jump to: <a href="2006ical.xml">2006</a>, <a 
href="2005ical.xml">2005</a>, <a href="2004ical.xml">2004</a>, <a 
href="2003ical.xml">2003</a>, <a href="2002ical.xml">2002</a>, <a href="2001ical.xml">2001</a></h4>
    <div align="right"><p>This uses <a href="http://kanzaki.com/works/2004/cal/concerts-tokyo.rdf">Masahide Kanzaki's RDF iCalendar XSLT and javascript</a>. <a href="about.html">More info here</a></p>
    </div>
    <xsl:apply-templates select="rdf:RDF"/>
   </body>
  </html>
 </xsl:template>

 <!-- main data handling -->
 <xsl:template match="rdf:RDF">
  <xsl:call-template name="events"/>
  <div id="components">
<img src="/parts/icals.gif" alt="components listed below..."/>
<xsl:call-template name="prevNext"/>
</div>
  <xsl:choose>
   <!-- standard RDFical -->
   <xsl:when test="i:Vcalendar">
    <xsl:apply-templates select="i:Vcalendar"/>
   </xsl:when>
   <!-- RSS with RDFical vocab -->
   <xsl:otherwise>
    <p><xsl:value-of select="rss:channel/rss:description"/></p>
    <h2 id="components">Event 'Components'</h2>
    <p>（Those events not belong to Vcalendar, but use its vocabulary）</p>
    <xsl:apply-templates select=".//i:Vevent|.//i:Vtimezone"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- if the file has Vcalendar element -->
 <xsl:template match="i:Vcalendar">
<!-- 
  <xsl:variable name="vpos" select="position()"/>
  <xsl:if test="i:component">
   <h2 id="components{$vpos}">Components<xsl:if test="*[local-name()='wrCalname']"> of &quot;<xsl:value-of select="*[local-name()='wrCalname']"/>&quot;</xsl:if></h2>
   <xsl:apply-templates select="i:component/*"/>
  </xsl:if>
  <h2 id="cal-prop{$vpos}">Calendar Properties</h2>
  <table class="cp" cellpadding="5">
   <xsl:apply-templates select="*[local-name() != 'component']"/>
  </table>
-->
 </xsl:template>

 <!-- Nested Property values -->
 <xsl:template match="*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:call-template name="propval"/>
 </xsl:template>

 <!-- Properties from ical will have hyperlink to definition HTML -->
 <xsl:template match="i:*">
  <dfn><a href="http://kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></dfn>: 
  <xsl:call-template name="propval"/>
 </xsl:template>

 <!-- Calender properties -->
 <xsl:template match="i:Vcalendar/*">
  <tr>
   <th><xsl:value-of select="name()"/></th>
   <td><xsl:call-template name="valOrRsrource"/></td>
  </tr>
 </xsl:template>
 <!-- Calender properties from ical samespace-->
 <xsl:template match="i:Vcalendar/i:*">
  <tr>
   <th><a href="http://kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></th>
   <td><xsl:call-template name="valOrRsrource"/></td>
  </tr>
 </xsl:template>

 <!-- Calendar Component Elements -->
 <!-- original:i:component/*, but changed to cover RSS with RDFical -->
 <xsl:template match="i:Vevent|i:Vtimezone|i:Valarm|i:Vtodo|i:Vjournal|i:Vfreebusy">
  <table border="1" cellspacing="0" cellpadding="5" class="component">
   <xsl:attribute name="id"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
   <caption><xsl:value-of select="local-name()"/> (<xsl:value-of select="position()"/>)</caption>
   <thead><tr><th class="property">Property</th><th class="value">Value</th></tr></thead>
   <tbody>
    <xsl:apply-templates select="i:dtstart"/>
    <xsl:apply-templates select="i:dtend"/>
    <xsl:apply-templates select="i:summary"/>
    <xsl:apply-templates select="i:description"/>
    <xsl:apply-templates select="*[local-name()!='dtstart' and local-name()!='dtend' and local-name()!='summary' and local-name()!='description']"/>
    <xsl:apply-templates select="../../rss:*"/>
   </tbody>
  </table>
 </xsl:template>


 <!-- apply from imgdesc 2004-01-27 -->
 <xsl:template name="propval">
  <xsl:choose>
   <xsl:when test="@rdf:nodeID[ancestor::i:Vevent]">
    <span class="f">(<xsl:call-template name="collectnodes">
     <xsl:with-param name="nid"><xsl:value-of select="@rdf:nodeID"/></xsl:with-param>
    </xsl:call-template>)</span>
   </xsl:when>
   <xsl:when test="@rdf:about[ancestor::i:Vevent]">
    <span class="f">(<xsl:call-template name="collectresourse">
     <xsl:with-param name="ab"><xsl:value-of select="@rdf:about"/></xsl:with-param>
    </xsl:call-template>)</span>
   </xsl:when>
   <xsl:when test="@*|*">
    <span class="f">(<xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="*"/>); </span>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="valOrRsrource"/>; 
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="collectnodes">
  <xsl:param name="nid"/>
  <!--<dfn>nodeID</dfn>:<xsl:value-of select="$nid"/>; -->
  <xsl:for-each select="//*[@rdf:nodeID=$nid]">
   <xsl:call-template name="valOrRsrource"/>; 
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="collectnodeelts">
  <xsl:param name="nid"/>
  <!--<dfn>nodeID</dfn>:<xsl:value-of select="$nid"/>; -->
  <xsl:for-each select="//*[@rdf:nodeID=$nid]">
   <xsl:if test="*">
    <xsl:call-template name="propval"/>; 
   </xsl:if>
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="collectresourse">
  <xsl:param name="ab"/>
  <!--<dfn>about</dfn>:<xsl:value-of select="$ab"/>; -->
  <xsl:variable name="anode" select="//*[@rdf:about=$ab]"/>
  <xsl:choose>
   <xsl:when test="count($anode) &gt; 1">
    <xsl:for-each select="$anode">
     <xsl:call-template name="valOrRsrource"/>; 
    </xsl:for-each>
   </xsl:when>
   <xsl:when test="starts-with($ab,'http:')">
    about: <a href="{$ab}"><xsl:value-of select="$ab"/></a>; 
   </xsl:when>
   <xsl:otherwise>
    <dfn>about</dfn>: <xsl:value-of select="$ab"/>; 
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="concatelt">
  <xsl:param name="d"/>
  <xsl:param name="d2"/>
  <xsl:for-each select="$d">
   <xsl:value-of select="."/><xsl:text> </xsl:text>
  </xsl:for-each>
  <xsl:for-each select="$d2">
   (<!--<xsl:if test="not(../@rdf:parseType)"><xsl:value-of select="local-name(..)"/>: </xsl:if>--><xsl:value-of select="."/>)<xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="foaf:mbox_sha1sum">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <acronym title="{.}"><xsl:value-of select="substring(.,1,6)"/>...</acronym> ; 
 </xsl:template>

 
 <!-- Attribute value -->
 <xsl:template match="@*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <xsl:template match="@rdf:nodeID|@rdf:parseType">
 </xsl:template>

 <xsl:template match="i:rrule/i:*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <xsl:template match="@i:*">
  <dfn><a href="http://kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <!-- Component properties from others -->
 <xsl:template match="i:component/*/*|i:Vevent/*|rss:item/rss:*">
  <tr>
   <th class="xterms"><xsl:value-of select="name()"/></th>
   <td>
    <xsl:call-template name="valOrRsrource"/>
   </td>
  </tr>
 </xsl:template>

 <xsl:template name="valOrRsrource">
  <xsl:choose>
   <xsl:when test="*">
    <xsl:apply-templates select="*"/>
   </xsl:when>
   <xsl:when test="@rdf:resource">
    <xsl:apply-templates select="@rdf:resource"/>
   </xsl:when>
   <!--test 2004-01-27 ancestor with this XSL target element-->
   <xsl:when test="@rdf:nodeID[ancestor::i:Vevent]">
    <xsl:call-template name="collectnodeelts">
     <xsl:with-param name="nid"><xsl:value-of select="@rdf:nodeID"/></xsl:with-param>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="."/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- Component properties from ical namespace -->
 <xsl:template match="i:Vevent/i:*|i:Vtimezone/i:*|i:component/*/i:*">
  <tr>
   <th>
    <a href="http://kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a>
   </th>
   <td>
    <xsl:call-template name="valOrRsrource"/>
    <xsl:call-template name="geomap"/>
   </td>
  </tr>
 </xsl:template>

 <!-- add a map button if contains geo: properties -->
 <xsl:template name="geomap">
  <xsl:if test=".//geo:lat">
   <button><xsl:attribute name="onclick"><xsl:value-of select="concat('showmap(',.//geo:lat,',',.//geo:long)"/>,'<xsl:value-of select="..//i:tzid"/>');</xsl:attribute>Map</button>
   <xsl:if test="contains($mkcateg,'Rehearsal')">
    <button><xsl:attribute name="onclick"><xsl:value-of select="concat('showmap(',.//geo:lat,',',.//geo:long)"/>,'<xsl:value-of select="..//i:tzid"/>','park');</xsl:attribute>P-Map</button>
   </xsl:if>
  </xsl:if>
 </xsl:template>

 <!-- Component properties: esp. dtstart and dtend -->
 <xsl:template match="i:component/*/i:dtstart|i:component/*/i:dtend|i:Vevent/i:dtstart|i:Vevent/i:dtend">
  <tr>
   <th class="dtse"><a href="http://kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></th>
   <td><xsl:apply-templates select="*"/></td>
  </tr>
 </xsl:template>


 <!-- Component property values: esp. date and dateTime -->
 <xsl:template match="i:date|i:dateTime">
  <xsl:value-of select="."/>
 </xsl:template>

 <!-- Component property value: esp. tzid -->
 <xsl:template match="i:tzid">
  (<dfn><a href="http://kanzaki.com/docs/ical/tzid.html"><xsl:value-of select="local-name()"/></a></dfn>: <xsl:value-of select="."/>)
 </xsl:template>

 <!-- Show URI refrence with readable format -->
 <xsl:template match="@rdf:resource">
  <xsl:choose>
   <xsl:when test="contains(.,'http://www.w3.org/2002/12/cal/ical#')">
    <em>#<xsl:value-of select="substring-after(.,'#')"/></em>
   </xsl:when>
   <xsl:when test="starts-with(.,'#')">
    <xsl:variable name="ab" select="substring(.,2)"/>
    <xsl:for-each select="//*[@rdf:ID=$ab]">
     <xsl:call-template name="valOrRsrource"/>; 
    </xsl:for-each>
   </xsl:when>
   <!--
   <xsl:when test="local-name(../.)='depiction'">
    <a href="{.}">image</a>
   </xsl:when>
   -->
   <xsl:otherwise>
    <xsl:call-template name="respoint"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="respoint">
  <xsl:variable name="ab" select="."/>
  <xsl:variable name="elt" select="name(../.)"/>
  <xsl:variable name="anode" select="//*[@rdf:about=$ab]"/>
  <xsl:choose>
   <xsl:when test="$elt='an:annotates'">
    <a href="{.}"><xsl:value-of select="."/></a>
   </xsl:when>
   <xsl:when test="$anode">
    <xsl:for-each select="$anode">
     <xsl:choose>
     <!-- test reffering to the same image -->
      <xsl:when test="local-name() = 'Image'"><a href="{@rdf:about}">image</a></xsl:when>
      <xsl:otherwise><xsl:call-template name="valOrRsrource"/>; </xsl:otherwise>
     </xsl:choose>
    </xsl:for-each>
   </xsl:when>
   <xsl:otherwise>
    <a href="{.}">
     <xsl:choose>
      <xsl:when test="string-length(.) &gt; 50"><xsl:value-of select="substring(.,1,48)"/>...</xsl:when>
      <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
     </xsl:choose>
    </a>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- Previous/Next calendar -->
 <xsl:template name="prevNext">
  <xsl:if test="i:Vcalendar/rs:prev">
   | <a id="refprev" href="{i:Vcalendar/rs:prev/@rdf:resource}">&lt;&lt; Previous calendar data</a> 
  </xsl:if>
  <xsl:if test="i:Vcalendar/rs:next">
   | <a id="refnext" href="{i:Vcalendar/rs:next/@rdf:resource}">Next calendar data &gt;&gt;</a> | 
  </xsl:if>
 </xsl:template>

 <!-- XHTML head elements -->
 <xsl:template name="htmlhead">
  <link rel="stylesheet" href="kan01.css" type="text/css" />
  <link rel="stylesheet" href="ical.css" type="text/css" />
  <style type="text/css">.f .f .f{font-size:small}</style>
  <script type="text/javascript" src="cal.js">//</script>
<!--
  <xsl:if test="//i:Vevent/i:geo">
   <script type="text/javascript" src="http://kanzaki.com/parts/geo.js">//</script>
  </xsl:if>
-->
 </xsl:template>

 <!-- Event Summary -->
 <xsl:template name="events">
  <xsl:variable name="ev" select=".//i:Vevent"/>
  <xsl:variable name="evc" select="count($ev)"/>
  <xsl:if test="$evc">
   <div id='mcal'>(If everything goes well, you'll find a monthly calendar here...)</div>

   <table id="allevents" border="1" cellspacing="0" cellpadding="3">
    <thead><tr><th>Starts</th><th>Ends</th><th>Rules</th><th>Summary</th><th>Location</th></tr></thead>
    <tbody id="slist">
    <xsl:for-each select="$ev">
     <xsl:sort select="i:dtstart/i:dateTime|i:dtstart/i:date"/>
     <tr class="{i:dtstart/i:dateTime|i:dtstart/i:date}">
<!--
      <td><a href="#{generate-id(.)}"><xsl:value-of select="i:dtstart/i:dateTime|i:dtstart/i:date"/></a>
      <xsl:value-of select="i:dtstart/i:dateTime|i:dtstart/i:date"/>
-->
      <td>
      <a href="{i:url/@rdf:resource}"><xsl:value-of select="i:dtstart/i:dateTime|i:dtstart/i:date"/></a>
      </td>
      <td><xsl:value-of select="i:dtend/i:dateTime|i:dtend/i:date"/></td>
      <td>
       <xsl:if test="i:rrule">
        <xsl:call-template name="evt-reccur">
         <xsl:with-param name="r" select="i:rrule"/>
        </xsl:call-template>
       </xsl:if>
      </td>
      <td><xsl:value-of select="i:summary|../../rss:title"/></td>
      <td><xsl:value-of select="p:highlight/@rdf:resource"/></td>

<!--
      <td><xsl:value-of select="i:location"/></td>
hmm....
-->
     </tr>
    </xsl:for-each>
    </tbody>
   </table>
  </xsl:if>
 </xsl:template>

 <xsl:template name="evt-reccur">
  <xsl:param name="r"/>
  <small>
  <xsl:value-of select="$r/i:freq"/> (
  <xsl:choose>
   <xsl:when test="$r/i:byday|$r/i:bymonth">
    <xsl:value-of select="$r/i:byday"/>
    <xsl:if test="$r/i:bymonth"> in month <xsl:value-of select="$r/i:bymonth"/></xsl:if>
    <xsl:if test="$r/i:count">: <xsl:value-of select="$r/i:count"/> time<xsl:if test="number($r/i:count)&gt;1">(s)</xsl:if></xsl:if>
    <xsl:if test="$r/i:until"> until <xsl:value-of select="$r/i:until"/></xsl:if>
   </xsl:when>
   <xsl:otherwise>*see reccurence rule</xsl:otherwise>
  </xsl:choose>
  )</small>
 </xsl:template>
 
 <!--<xsl:include href="./calscript.xsl"/>-->
<!-- <xsl:include href="./banner-footer.xsl"/>-->

</xsl:stylesheet>

