<?xml version="1.0"?>
<xsl:stylesheet 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:foaf="http://xmlns.com/foaf/0.1/"
                xmlns:air="http://www.megginson.com/exp/ns/airports#"
                xmlns:con="http://www.w3.org/2000/10/swap/pim/contact#"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:wot="http://xmlns.com/wot/0.1/"
                xmlns:wn="http://xmlns.com/wordnet/1.6/"
                version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">
  <html>
    <head>
      <title><xsl:value-of select="//dc:title[1]"/></title>
      <style type="text/css">
a.footnote { text-decoration: none; }
      </style>
    </head>
    <body>
      <xsl:apply-templates/>
      <br clear="all"/>
      <hr/>
      <table border="0">
        <tr>
          <td align="left" valign="top"><a name="footnote"/><sup>*</sup></td>
          <td align="left" valign="top">
            <xsl:text>The transformed, </xsl:text>
            <q>friendly</q>
            <xsl:text> view of this </xsl:text>
            <a href="http://rdfweb.org/foaf/">friend of a friend</a>
            <xsl:text> information is </xsl:text>
            <em>almost certainly</em>
            <xsl:text> incomplete. For the real story, </xsl:text>
            <q>Use the source, Luke</q>
            <xsl:text>.</xsl:text>
          </td>
        </tr>
      </table>
    </body>
  </html>
</xsl:template>

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

<xsl:template match="rdf:Description">
  <xsl:apply-templates/>
  <xsl:if test="rdfs:seeAlso">
    <div class="seealso">
      <p>See also:</p>
      <ul>
        <xsl:apply-templates select="rdfs:seeAlso" mode="list"/>
      </ul>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="foaf:Person">
  <div>
    <xsl:apply-templates/>
  </div>
</xsl:template>

<xsl:template match="/rdf:RDF/rdf:Description[1]/dc:title[1]">
  <h1>
    <xsl:apply-templates/>
    <sup>
      <a class="footnote" href="#footnote">*</a>
    </sup>
  </h1>
</xsl:template>

<xsl:template match="dc:description">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match="wot:assurance">
  <p>
    <xsl:text>The RDF source for this document </xsl:text>
    <xsl:choose>
      <xsl:when test="../wot:sigdate">
        <xsl:text>was </xsl:text>
        <a href="{@rdf:resource}">signed</a>
        <xsl:text> on </xsl:text>
        <xsl:value-of select="../wot:sigdate"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>is </xsl:text>
        <a href="{@rdf:resource}">signed</a>
      </xsl:otherwise>
    </xsl:choose>
  </p>
</xsl:template>

<xsl:template match="wot:sigdate">
  <!-- suppress -->
</xsl:template>

<xsl:template match="foaf:Person">
  <div class="person">
    <hr/>
    <xsl:if test="foaf:depiction">
      <img align="right" src="{foaf:depiction[1]/@rdf:resource}"
           alt="Depiction"/>
    </xsl:if>
    <h2>
      <xsl:value-of select="foaf:name[1]"/>
    </h2>
    <p>
      <xsl:value-of select="foaf:name[1]"/>
      <xsl:if test="foaf:nick">
        <xsl:text> (</xsl:text>
        <xsl:apply-templates select="foaf:nick"/>
        <xsl:text>)</xsl:text>
      </xsl:if>
      <xsl:text> is </xsl:text>
      <xsl:apply-templates select="foaf:mbox"/>
      <xsl:text>.</xsl:text>
    </p>
    <h3>Info about <xsl:value-of select="foaf:nick[1]"/>:</h3>
    <ul>
      <xsl:apply-templates select="foaf:homepage"/>
      <xsl:apply-templates select="foaf:workplaceHomepage"/>
      <xsl:apply-templates select="foaf:pubkeyAddress"/>
      <xsl:apply-templates select="con:nearestAirport"/>
      <li>
        <xsl:text>Interests:</xsl:text>
        <ul>
          <xsl:apply-templates select="foaf:interest"/>
        </ul>
      </li>
      <li>
        <xsl:text>Knows:</xsl:text>
        <ul>
          <xsl:apply-templates select="foaf:knows"/>
        </ul>
      </li>
    </ul>
  </div>
</xsl:template>

<xsl:template match="foaf:nick">
  <xsl:apply-templates/>
  <xsl:if test="following-sibling::foaf:nick">
    <xsl:text>, </xsl:text>
  </xsl:if>
</xsl:template>


<xsl:template match="foaf:nick">
  <xsl:apply-templates/>
  <xsl:if test="following-sibling::foaf:nick">
    <xsl:text>, </xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="foaf:mbox">
  <xsl:variable name="mboxCount" select="count(../foaf:mbox)"/>
  <xsl:variable name="mbox">
    <xsl:choose>
      <xsl:when test="starts-with(@rdf:resource, 'mailto:')">
        <xsl:value-of select="substring-after(@rdf:resource, 'mailto:')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@rdf:resource"/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- should be empty... -->
    <xsl:apply-templates/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="@rdf:resource">
      <a href="{@rdf:resource}">
        <xsl:copy-of select="$mbox"/>
      </a>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="$mbox"/>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:if test="following-sibling::foaf:mbox">
    <xsl:if test="$mboxCount &gt; 2">, </xsl:if>
    <xsl:if test="count(following-sibling::foaf:mbox) = 1"> and </xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template match="foaf:homepage">
  <li>
    <xsl:text>Homepage: </xsl:text>
    <a href="{@rdf:resource}">
      <xsl:value-of select="@rdf:resource"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="foaf:workplaceHomepage">
  <li>
    <xsl:text>Workplace homepage: </xsl:text>
    <a href="{@rdf:resource}">
      <xsl:value-of select="@rdf:resource"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="foaf:pubkeyAddress">
  <li>
    <xsl:text>PGP Public key: </xsl:text>
    <a href="{@rdf:resource}">
      <xsl:value-of select="@rdf:resource"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="foaf:knows">
  <li>
    <xsl:value-of select=".//foaf:name[1]"/>
    <xsl:if test=".//foaf:page">
      <xsl:text>&#160;</xsl:text>
      <a href="{.//foaf:page[1]/@rdf:resource}">
        <img src="images/foafTiny.gif" alt="(foaf)" border="0"/>
      </a>
    </xsl:if>
  </li>
</xsl:template>

<xsl:template match="foaf:interest">
  <li>
    <a href="{@rdf:resource}">
      <xsl:value-of select="@rdf:resource"/>
    </a>
  </li>
</xsl:template>

<xsl:template match="con:nearestAirport">
  <xsl:variable name="iata" select=".//@air:iata[1]"/>
  <xsl:if test="$iata">
    <li>
      <xsl:text>Nearest airport: </xsl:text>
      <a href="http://www.ar-group.com/Airport-Locator.asp?RequestLocation={$iata}">
        <xsl:value-of select="$iata"/>
      </a>
    </li>
  </xsl:if>
</xsl:template>

<xsl:template match="rdfs:seeAlso" mode="list">
  <li>
    <a href="{@rdf:resource}">
      <xsl:value-of select="@rdf:resource"/>
    </a>
  </li>
</xsl:template>

</xsl:stylesheet>
