<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns="http://www.w3.org/1999/xhtml"
		xmlns:html="http://www.w3.org/1999/xhtml"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:owl="http://www.w3.org/2002/07/owl#"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
		exclude-result-prefixes="html dc owl rdf rdfs"
                version="1.0">

<xsl:output method="html" encoding="utf-8" indent="no"
	    omit-xml-declaration="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="domain" match="*" use="rdfs:domain/@rdf:resource"/>
<xsl:key name="class" match="owl:Class" use="@rdf:about"/>

<xsl:param name="ns" select="'http://www.w3.org/2006/vcard/ns#'"/>

<xsl:template match="/">
  <html>
    <head>
      <title>
	<xsl:value-of select="/rdf:RDF/owl:Ontology/dc:title"/>
      </title>
      <link rel="stylesheet" type="text/css" href="ns.css"/>
    </head>
    <body>
      <p class="warning">[N.B. This document is generated automatically
from the <a href="ns.rdf">RDF ontology</a> (<a href="ns.n3">N3</a>). It is
likely to be incomplete. Some day there will be a proper specification.]</p>

      <xsl:apply-templates select="/rdf:RDF/owl:Ontology"/>

      <div class="toc">
	<h3>Table of Contents</h3>
	<dl>
	  <xsl:for-each select="/rdf:RDF/owl:Class">
	    <xsl:sort select="@rdf:about" order="descending"/>
	    <dt>
	      <a href="#{substring-after(@rdf:about,'#')}">
		<xsl:value-of select="rdfs:label"/>
	      </a>
	    </dt>
	  </xsl:for-each>
	  <dt><a href="#property-index">Property Index</a></dt>
	</dl>
      </div>

      <xsl:apply-templates select="/rdf:RDF/owl:Class">
	<xsl:sort select="@rdf:about" order="descending"/>
      </xsl:apply-templates>

      <hr/>
      <div class="index">
	<h2>
	  <a name="property-index" id="property-index"/>
	  <xsl:text>Property Index</xsl:text>
	</h2>
	<p>
	  <xsl:for-each select="//owl:ObjectProperty|//owl:DatatypeProperty">
	    <xsl:sort select="@rdf:about" order="ascending"/>

	    <a href="#{substring-after(@rdf:about,'#')}">
	      <xsl:value-of select="substring-after(@rdf:about,'#')"/>
	    </a>
	    <xsl:if test="position() &lt; last()">, </xsl:if>
	  </xsl:for-each>
	</p>
      </div>
    </body>
  </html>
</xsl:template>

<xsl:template match="owl:Ontology">
  <div class="ontology">
    <xsl:apply-templates select="dc:title"/>
    <xsl:apply-templates select="dc:description"/>
  </div>
</xsl:template>

<xsl:template match="owl:Class">
  <hr/>
  <div class="class">
    <xsl:apply-templates select="rdfs:label"/>
    <xsl:apply-templates select="rdfs:comment"/>

    <h3>Properties</h3>

    <ul class="properties">
      <xsl:apply-templates select="key('domain',@rdf:about)">
	<xsl:sort select="@rdf:about"/>
      </xsl:apply-templates>
    </ul>
  </div>
</xsl:template>

<xsl:template match="owl:ObjectProperty|owl:DatatypeProperty">
  <xsl:variable name="pname"
		select="substring-after(@rdf:about,$ns)"/>

  <xsl:variable name="label"
		select="rdfs:label"/>

  <li class="property">
    <a id="{$pname}" name="{$pname}"/>
    <code>
      <xsl:value-of select="$pname"/>
    </code>

    <xsl:if test="$pname != $label">
      <xsl:text> (</xsl:text>
      <xsl:value-of select="$label"/>
      <xsl:text>)</xsl:text>
    </xsl:if>

    <xsl:variable name="restrictions"
		  select="key('class',rdfs:domain/@rdf:resource)/rdfs:subClassOf[rdf:type/@rdf:resource='http://www.w3.org/2002/07/owl#Restriction' and owl:onProperty[@rdf:resource=current()/@rdf:about]]"/>

    <xsl:if test="rdfs:domain|rdfs:range|$restrictions">
      <ul>
	<xsl:apply-templates select="rdfs:domain"/>
	<xsl:apply-templates select="rdfs:range"/>
	<xsl:apply-templates select="$restrictions/*" mode="restrictions"/>
      </ul>
    </xsl:if>

    <xsl:apply-templates select="rdfs:comment"/>
  </li>
</xsl:template>

<!-- ============================================================ -->

<xsl:template match="rdf:type|owl:onProperty" mode="restrictions"/>

<xsl:template match="*" mode="restrictions">
  <li>
    <xsl:value-of select="local-name(.)"/>
    <xsl:text> = </xsl:text>
    <xsl:value-of select="."/>
  </li>
</xsl:template>

<!-- ============================================================ -->

<xsl:template match="dc:title">
  <h1>
    <xsl:apply-templates/>
  </h1>
</xsl:template>

<xsl:template match="owl:Class/rdfs:label">
  <xsl:variable name="id" select="substring-after(../@rdf:about,'#')"/>
  <h2>
    <a id="{$id}" name="{$id}"/>
    <xsl:apply-templates/>
  </h2>
</xsl:template>

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

<xsl:template match="rdfs:domain">
  <li>
    <xsl:text>Domain: </xsl:text>
    <code>
      <xsl:apply-templates select="@rdf:resource"/>
    </code>
  </li>
</xsl:template>

<xsl:template match="rdfs:range">
  <li>
    <xsl:text>Range: </xsl:text>
    <code>
      <xsl:apply-templates select="@rdf:resource"/>
    </code>
  </li>
</xsl:template>

<xsl:template match="@rdf:resource">
  <xsl:variable name="href">
    <xsl:choose>
      <xsl:when test="starts-with(.,$ns)">
	<xsl:text>#</xsl:text>
	<xsl:value-of select="substring-after(.,$ns)"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <a href="{$href}">
    <xsl:value-of select="."/>
  </a>
</xsl:template>

</xsl:stylesheet>

