<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:wot="http://xmlns.com/wot/0.1/"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:dcterms="http://purl.org/dc/terms/"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  exclude-result-prefixes="wot rdfs rdf dc dcterms foaf"
  >
 <!-- dcterms foaf wn wot-->
 <xsl:output indent="yes"/>

 <xsl:template name="notice">
 <!-- an explanatory message-->
  <div class="lead" style="margin-left:3em;margin-right:2em" lang="en">
   <h2>Welcome and enjoy</h2>
   <p>This stylesheet is designed to convert FOAF file to visible XHTML table format.</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) 2002-2008 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-2008 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:template match="/">
  <html>
   <head>
    <title>FOAF metadata of <xsl:value-of select="rdf:RDF/foaf:Person/foaf:name"/> - XHTML rendering</title>
    <link rel="stylesheet" href="/parts/kan01.css" type="text/css" />
    <style type="text/css">table {width:100%} .prop {width:20%} .interest,.pastProject,.made {background:#eef} .currentProject,.publications {background:#fee}</style>
    <script type="text/javascript">function validate(){location.href= "http://www.w3.org/RDF/Validator/ARPServlet?URI=" + document.location;}</script>
   </head>
   <body>
    <xsl:call-template name="banner"/>
    <h1><img src="/parts/foaf-l.gif" alt="FOAF" /> Metadata of <xsl:value-of select="rdf:RDF/foaf:Person/foaf:name"/></h1>
    <p>This is a visual presentation of <a href="/docs/sw/foaf.html"><abbr title="Friend of a Friend">FOAF</abbr></a> metadata via XSLT. See source for its XML serialization.</p>
	
	<xsl:choose>
	 <xsl:when test="rdf:RDF/foaf:PersonalProfileDocument">
      <xsl:apply-templates select="rdf:RDF/foaf:PersonalProfileDocument"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:apply-templates select="rdf:RDF/foaf:Person[1]"/>
     </xsl:otherwise>
    </xsl:choose>

    <p><input type="image" src="/parts/rdf_metadata.gif" alt="RDF Meadata" class="sign" title="Validate and show Graph" onclick="validate()" /> Note: This metadata is presented for a visual user agent via XSLT.</p>
    <hr />
    <address>
     <xsl:apply-templates select="rdf:RDF/foaf:Document"/>
     The XSLT is <a href="/info/disclaimer">&#x00A9;2002-2004 by MK</a>, and <a href="http://www.gnu.org/licenses/gpl.html">GPL</a> is applied.
    </address>
   </body>
  </html>
 </xsl:template>

 <!-- PersonalProfileDocument -->
 <xsl:template match="foaf:PersonalProfileDocument">
  <xsl:if test="dcterms:modified">
   <p>This information is updated on <xsl:value-of select="dcterms:modified"/></p>
  </xsl:if>
  <xsl:choose>
   <xsl:when test="foaf:primaryTopic[@rdf:resource]">
    <xsl:variable name="r" select="foaf:primaryTopic/@rdf:resource"/>
    <xsl:apply-templates select="/rdf:RDF//foaf:Person[@rdf:about=$r or @rdf:ID=substring-after($r,'#')]"/>
   </xsl:when>
   <xsl:when test="foaf:primaryTopic[@rdf:nodeID]">
    <xsl:apply-templates select="/rdf:RDF//foaf:Person[@rdf:nodeID=foaf:primaryTopic/@rdf:nodeID]"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:apply-templates select="rdf:RDF/foaf:Person[1]"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- the topmost foaf:Person element -->
 <xsl:template match="foaf:Person">
  <table border="1" cellpadding="5" cellspacing="0">
   <caption>FOAF Elements described here</caption>
   <thead>
    <tr><th class="prop">Property</th><th>Value</th></tr>
   </thead>
   <tbody>
    <xsl:apply-templates select="*[local-name()!='knows']"/>
   </tbody>
  </table>
  <xsl:if test="./foaf:sortName">
   <p>* SortName is a <a href="http://www.wasab.dk/morten/2003/04/foaf-names-proposal-1.html">proposed element</a> by Morten Frederiksen.</p>
  </xsl:if>
  <h2>and I know ...</h2>
  <table border="1" cellpadding="5" cellspacing="0">
   <caption>Information on my friends</caption>
   <thead>
    <tr><th>Name</th><th>Description</th></tr>
   </thead>
   <tbody>
    <xsl:apply-templates select="*[local-name()='knows']"/>
   </tbody>
  </table>
 </xsl:template>


 <!-- general foaf elements w/wo attributes -->
 <xsl:template match="*">
  <tr class="{local-name()}">
   <td>
    <xsl:choose>
     <xsl:when test="namespace-uri='http://xmlns.com/foaf/0.1/'"><xsl:value-of select="local-name()"/></xsl:when>
     <xsl:otherwise><xsl:value-of select="name()"/></xsl:otherwise>
    </xsl:choose>
   </td>
   <td>
    <xsl:if test="@*"><xsl:apply-templates select="@*" mode="child"/></xsl:if>
    <xsl:choose>
     <xsl:when test="*"><xsl:apply-templates select="*" mode="child"/></xsl:when>
     <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
    </xsl:choose>
   </td>
  </tr>
 </xsl:template>

 <xsl:template match="*|@*" mode="child">
  <xsl:if test="local-name() != 'parseType' and local-name() != 'about'">
   <xsl:value-of select="local-name()"/> =&gt;
   <xsl:choose>
    <xsl:when test="* or @*">(<xsl:apply-templates select="*|@*" mode="child"/>)</xsl:when>
    <xsl:otherwise><xsl:value-of select="."/>; </xsl:otherwise>
   </xsl:choose>
  </xsl:if>
 </xsl:template>

 <xsl:template match="*[@rdf:resource]">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/></td>
   <td><a href="{./@rdf:resource}"><xsl:value-of select="./@rdf:resource"/></a></td>
  </tr>
 </xsl:template>

 <xsl:template match="*[@rdf:resource]" mode="child">
  <a href="{./@rdf:resource}"><xsl:value-of select="local-name()"/></a>; 
 </xsl:template>

 <xsl:template match="*[@rdf:about]" mode="child">
  <a href="{./@rdf:about}"><xsl:value-of select="local-name()"/></a> 
  <xsl:if test="* or @*"> =&gt; (<xsl:apply-templates select="*|@*" mode="child"/>)</xsl:if>
 </xsl:template>

 <xsl:template match="*[@xml:lang]">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/> (<xsl:value-of select="./@xml:lang"/>)</td>
   <td><xsl:value-of select="."/></td>
  </tr>
 </xsl:template>

 <xsl:template match="*[@dc:title or @rdfs:label]">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/></td>
   <xsl:choose>
    <xsl:when test="starts-with(@dc:identifier,'urn:isbn')">
     <td><xsl:value-of select="./@dc:title"/><xsl:apply-templates select="./@dc:language"/> <a><xsl:attribute name="href">http://www.amazon.co.jp/exec/obidos/ASIN/<xsl:value-of select="translate(substring(@dc:identifier,10),'-','')"/>/thewebkanzaki-22</xsl:attribute><xsl:value-of select="./@dc:identifier"/></a></td>
    </xsl:when>
    <xsl:otherwise>
     <td><a href="{./@rdf:resource}"><xsl:value-of select="./@dc:title"/><xsl:value-of select="./@rdfs:label"/></a><xsl:apply-templates select="./@dc:language"/></td>
    </xsl:otherwise>
   </xsl:choose>
  </tr>
 </xsl:template>

 <xsl:template match="foaf:mbox[@rdf:resource]">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/></td>
   <td>
    <xsl:choose>
     <xsl:when test="@rdf:resource=concat('mailto:webmas','ter@kanza','ki.com')">
    webmaster@... (See <a href="http://www.kanzaki.com/info/disclaimer#webmastermail">note on webmaster mail address</a>.)
     </xsl:when>
     <xsl:otherwise>
      <a href="{./@rdf:resource}"><xsl:value-of select="substring-before(substring-after(./@rdf:resource,':'),'@')"/>@<xsl:value-of select="substring(substring-after(./@rdf:resource,'@'),1,4)"/>...</a> (<xsl:value-of select="./@rdfs:comment"/>)
     </xsl:otherwise>
    </xsl:choose>
   </td>
  </tr>
 </xsl:template>

 <xsl:template match="@dc:language">
  <xsl:text> (</xsl:text><xsl:value-of select="."/>)
 </xsl:template>

 <xsl:template match="foaf:nick[.='masaka'][../@rdf:about='http://purl.org/net/who/kanzaki#masahide']">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/></td>
   <td><a href="http://www.kanzaki.com/info/masaka.html"><xsl:value-of select="."/></a></td>
  </tr>
 </xsl:template>

 <!-- special for proposed foaf:sortName -->
 <xsl:template match="foaf:sortName">
  <tr class="{local-name()}">
   <td><xsl:value-of select="local-name()"/> <sup> *</sup></td>
   <td><xsl:apply-templates select="./rdf:li/*"/></td>
  </tr>
 </xsl:template>

 <xsl:template match="foaf:sortName/rdf:li/*">
  <xsl:value-of select="local-name()"/> =&gt; <xsl:value-of select="./@rdf:value"/><xsl:text>; </xsl:text>
 </xsl:template>


 <!-- special for friends (knows) -->
 <xsl:template match="foaf:knows">
  <tr>
   <td><a href="{./foaf:Person/foaf:homepage/@rdf:resource|./foaf:Person/foaf:weblog/@rdf:resource}"><xsl:apply-templates select="./foaf:Person/foaf:name"/><xsl:apply-templates select="./foaf:Person/foaf:nick"/></a></td>
   <td><xsl:value-of select="./foaf:Person/dc:description"/></td>
  </tr>
 </xsl:template>

 <xsl:template match="foaf:knows//foaf:name">
  <xsl:choose>
   <xsl:when test="position()=1">
    <xsl:value-of select="."/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:text> (</xsl:text><xsl:value-of select="."/>)
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
 <xsl:template match="foaf:knows//foaf:nick">
  <xsl:text> (</xsl:text><xsl:value-of select="."/>)
 </xsl:template>
 
 <xsl:template match="rdf:RDF/foaf:Document">
  This is <xsl:value-of select="dc:title"/>,
  <xsl:apply-templates select="dc:date|dcterms:*"/>
  <xsl:apply-templates select="wot:assurance"/>.
 </xsl:template>

 <xsl:template match="foaf:Document/dc:date|foaf:Document/dcterms:*">
  <xsl:value-of select="local-name()"/>: <xsl:value-of select="."/>; 
 </xsl:template>

 <xsl:template match="foaf:Document/wot:assurance">
  <a href="{./@rdf:resource}">signed digitally</a>
 </xsl:template>

 <xsl:include href="../parts/banner-footer.xsl"/>

</xsl:stylesheet>