<?xml version="1.0"?>
<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:ver="http://www.ldodds.com/ns/ver/"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xmlns:dc="http://purl.org/dc/elements/1.1/">

<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:template match='rdf:RDF'>
    <html>
        <head>
            <title><xsl:value-of select="owl:Ontology/dc:title"/></title>
            <link rel="stylesheet" href="foaf.css" type="text/css" />
            <style type="text/css" media="screen">
                dt {
                    font-weight: bold;
                }

                .class-table {
                    margin: 1px;
                    padding: 3px;
                }

                .tableheading     { background: #CCCCFF; font-weight: bold; font-size: 120%;} /* Dark mauve */
            </style>
        </head>

        <body>

        <xsl:apply-templates select="owl:Ontology"/>
        
        <hr />

        <p>
        <table class="class-table" border="1" cellspacing="3" cellpadding="3" width="100%">
            <tr class="tableheading">
                <td colspan="2">Class Summary</td>
            </tr>
            <xsl:for-each select="rdfs:Class">
                <xsl:sort select="@rdf:about"/>
                <xsl:apply-templates select="." mode="summary"/>
            </xsl:for-each>
        </table>
        </p>

        <p>
        <table class="property-table" border="1" cellspacing="3" cellpadding="3" width="100%">
            <tr class="tableheading">
                <td colspan="3">Property Summary</td>
            </tr>
            <xsl:for-each select="rdf:Property">
                <xsl:sort select="@rdf:about"/>
                <xsl:apply-templates select="." mode="summary"/>
            </xsl:for-each>
        </table>
        </p>

        <p>
            <a name="class-detail"/>
            <table border="1" cellpadding="3" cellspacing="0" width="100%">
            <tr CLASS="tableheadingcolor">
            <td colspan="1" class="tableheading">Class Detail</td>
            </tr>
            </table>
        </p>

        <xsl:for-each select="rdfs:Class">
            <xsl:sort select="@rdf:about"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>

        <p>
            <a name="class-detail"/>
            <table border="1" cellpadding="3" cellspacing="0" width="100%">
            <tr CLASS="tableheadingcolor">
            <td colspan="1" class="tableheading">Property Detail</td>
            </tr>
            </table>
        </p>

        <xsl:for-each select="rdf:Property">
            <xsl:sort select="@rdf:about"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>
        
        </body>
    </html>
    </xsl:template>


    <!-- OWL stuff -->
    <xsl:template match="owl:Ontology">
        <h2>Title: <xsl:value-of select="dc:title"/></h2>

        <p><xsl:value-of select="dc:description"/></p>

        <dl>
            <dt>Date:</dt><dd><xsl:value-of select="dc:date"/></dd>
        </dl>
        <dl>
            <dt>Namespace:</dt><dd><a href="{@rdf:about}"><xsl:value-of select="@rdf:about"/></a></dd>
        </dl>
        <dl>
            <dt>Imports:</dt>
            <dd>
            <xsl:for-each select="owl:imports">
                <xsl:call-template name="anchor-rdf-resource">
                    <xsl:with-param name="node" select="."/>
                </xsl:call-template>
                <xsl:choose>
                    <xsl:when test="following-sibling::owl:imports">, </xsl:when>
                </xsl:choose>
            </xsl:for-each>
            </dd>
        </dl>
        <dl>
            <dt>See Also:</dt>
            <dd>
            <xsl:for-each select="rdfs:seeAlso">
                <xsl:call-template name="anchor-rdf-resource">
                    <xsl:with-param name="node" select="."/>
                </xsl:call-template>
                <xsl:choose>
                    <xsl:when test="following-sibling::rdfs:seeAlso">, </xsl:when>
                </xsl:choose>
            </xsl:for-each>
            </dd>
        </dl>
    </xsl:template>

    <!-- RDFS stuff -->

    <xsl:template match="rdfs:Class" mode="summary">
        <xsl:variable name="anchor">
            <xsl:value-of select="generate-id(.)"/>
        </xsl:variable>
    <tr>
        <td class="name">
            <a href="#{$anchor}">
                <xsl:call-template name="substring-after-last">
                     <xsl:with-param name="input" select="@rdf:about" />
                     <xsl:with-param name="marker" select="'/'" />
                </xsl:call-template>
            </a>
        </td>

        <td class="comment"><xsl:value-of select="@rdfs:comment"/></td>
    </tr>
    </xsl:template>

    <xsl:template match="rdf:Property" mode="summary">
        <xsl:variable name="anchor">
            <xsl:value-of select="generate-id(.)"/>
        </xsl:variable>
    <tr>
        <td class="name">
            <a href="#{$anchor}">
                <xsl:call-template name="substring-after-last">
                     <xsl:with-param name="input" select="@rdf:about" />
                     <xsl:with-param name="marker" select="'/'" />
                </xsl:call-template>
            </a>
        </td>
        <td class="comment"><xsl:value-of select="@rdfs:comment"/></td>
    </tr>
    </xsl:template>

    <!-- RDFS detailed templates -->

    <xsl:template match="rdfs:Class">

        <h3>
            <a name="{generate-id(.)}"></a>
                <xsl:call-template name="substring-after-last">
                     <xsl:with-param name="input" select="@rdf:about" />
                     <xsl:with-param name="marker" select="'/'" />
                </xsl:call-template>
        </h3>

        <p>
        
        <xsl:choose>
        <xsl:when test="rdfs:subClassOf">
        <dl>
            <dt>Subclass Of:</dt>
            <dd>
            <xsl:for-each select="rdfs:subClassOf">
                <xsl:call-template name="anchor-rdf-resource">
                    <xsl:with-param name="node" select="."/>
                </xsl:call-template>
                <xsl:choose>
                    <xsl:when test="following-sibling::rdfs:subClassOf">, </xsl:when>
                </xsl:choose>
            </xsl:for-each>
            </dd>
        </dl>
        </xsl:when>
        </xsl:choose>
        
        <xsl:choose>
        <xsl:when test="owl:disjointWith">
        <dl>
            <dt>Disjoint With:</dt>
            <dd>
            <xsl:for-each select="owl:disjointWith">
                <xsl:variable name="anchor-text">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, /owl:Ontology/@rdf:about)">
                            <xsl:call-template name="substring-after-last">
                                 <xsl:with-param name="input" select="@rdf:resource" />
                                 <xsl:with-param name="marker" select="'/'" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="anchor-href">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, /owl:Ontology/@rdf:about)">
                            <xsl:variable name="res" select="@rdf:resource"/>
                            <xsl:value-of select="concat('#', generate-id(//rdfs:Class/@rdf:about[. = $res]/..) )"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a href="{$anchor-href}"><xsl:value-of select="$anchor-text"/></a>
                <xsl:choose>
                    <xsl:when test="following-sibling::owl:disjointWith">, </xsl:when>
                </xsl:choose>
            </xsl:for-each>
            </dd>
        </dl>
        </xsl:when>
        </xsl:choose>
        
        </p>
        
        <p>
        <xsl:value-of select="@rdfs:comment"/>
        </p>

        <hr />
    </xsl:template>

    <xsl:template match="rdf:Property">
    
        <h3>
            <a name="{generate-id(.)}"></a>
                <xsl:call-template name="substring-after-last">
                     <xsl:with-param name="input" select="@rdf:about" />
                     <xsl:with-param name="marker" select="'/'" />
                </xsl:call-template>
        </h3>


        <xsl:choose>
        <xsl:when test="rdfs:domain">
        <dl>
            <dt>Domain:</dt>
            <dd>
            <xsl:for-each select="rdfs:domain">
                <xsl:variable name="anchor-text">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:call-template name="substring-after-last">
                                 <xsl:with-param name="input" select="@rdf:resource" />
                                 <xsl:with-param name="marker" select="'/'" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="anchor-href">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:variable name="res" select="@rdf:resource"/>
                            <xsl:value-of select="concat('#', generate-id(//rdfs:Class/@rdf:about[. = $res]/..) )"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a href="{$anchor-href}"><xsl:value-of select="$anchor-text"/></a>
                <xsl:choose>
                    <xsl:when test="following-sibling::rdfs:domain">, </xsl:when>
                </xsl:choose>

            </xsl:for-each>
            </dd>
        </dl>
        </xsl:when>
        </xsl:choose>

        <xsl:choose>
        <xsl:when test="rdf:subClassOf">
        <dl>
            <dt>Sub Class Of:</dt>
            <dd>
            <xsl:for-each select="rdfs:subClassOf">
                <xsl:variable name="anchor-text">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:call-template name="substring-after-last">
                                 <xsl:with-param name="input" select="@rdf:resource" />
                                 <xsl:with-param name="marker" select="'/'" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="anchor-href">
                    <xsl:choose>
                        <xsl:when test="starts-with(@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:variable name="res" select="@rdf:resource"/>
                            <xsl:value-of select="concat('#', generate-id(//rdfs:Class/@rdf:about[. = $res]/..) )"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a href="{$anchor-href}"><xsl:value-of select="$anchor-text"/></a>
                <xsl:choose>
                    <xsl:when test="following-sibling::rdfs:subClassOf">, </xsl:when>
                </xsl:choose>

            </xsl:for-each>
            </dd>
        </dl>
        </xsl:when>
        </xsl:choose>
        
        <xsl:choose>
        <xsl:when test="rdfs:range">
        <dl>
         <dt>Range:</dt>
         <dd>
                <xsl:variable name="anchor-text">
                    <xsl:choose>
                        <xsl:when test="starts-with(rdfs:range/@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:call-template name="substring-after-last">
                                 <xsl:with-param name="input" select="rdfs:range/@rdf:resource" />
                                 <xsl:with-param name="marker" select="'/'" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="rdfs:range/@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="anchor-href">
                    <xsl:choose>
                        <xsl:when test="starts-with(rdfs:range/@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:variable name="res" select="rdfs:range/@rdf:resource"/>
                            <xsl:value-of select="concat('#', generate-id(//rdfs:Class/@rdf:about[. = $res]/..) )"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="rdfs:range/@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a href="{$anchor-href}"><xsl:value-of select="$anchor-text"/></a>
         </dd>
        </dl>
        </xsl:when>
        </xsl:choose>
        
        <xsl:choose>
        <xsl:when test="rdfs:subPropertyOf">
        <dl>
         <dt>Sub Property Of:</dt>
         <dd>
                <xsl:variable name="anchor-text">
                    <xsl:choose>
                        <xsl:when test="starts-with(rdfs:subPropertyOf/@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:call-template name="substring-after-last">
                                 <xsl:with-param name="input" select="rdfs:subPropertyOf/@rdf:resource" />
                                 <xsl:with-param name="marker" select="'/'" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="rdfs:subPropertyOf/@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="anchor-href">
                    <xsl:choose>
                        <xsl:when test="starts-with(rdfs:subPropertyOf/@rdf:resource, //owl:Ontology/@rdf:about)">
                            <xsl:variable name="res" select="rdfs:subPropertyOf/@rdf:resource"/>
                            <xsl:value-of select="concat('#', generate-id(//rdf:Property/@rdf:about[. = $res]/..) )"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="rdfs:subPropertyOf/@rdf:resource"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a href="{$anchor-href}"><xsl:value-of select="$anchor-text"/></a>
         </dd>
        </dl>
        </xsl:when>
        </xsl:choose>
        
        <p>
         <xsl:value-of select="@rdfs:comment"/>
        </p>        
        <hr />
    </xsl:template>


    <!-- UTILITY TEMPLATES -->

    <!-- See: http://www.dpawson.co.uk/xsl/sect2/N7240.html#d7594e463 -->
    <xsl:template name="substring-after-last">
        <xsl:param name="input" />
        <xsl:param name="marker" />

        <xsl:choose>
          <xsl:when test="contains($input,$marker)">
            <xsl:call-template name="substring-after-last">
              <xsl:with-param name="input"
                  select="substring-after($input,$marker)" />
              <xsl:with-param name="marker" select="$marker" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
           <xsl:value-of select="$input" />
          </xsl:otherwise>
         </xsl:choose>

    </xsl:template>

    <xsl:template name="anchor-rdf-resources">
        <xsl:param name="nodes"/>
        <xsl:param name="div-class"/>

        <xsl:for-each select="$nodes">
            <div>
                <xsl:choose>
                    <xsl:when test="$div-class">
                        <xsl:attribute name="id"><xsl:value-of select="$div-class"/></xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise/>
                </xsl:choose>
                <xsl:call-template name="anchor-rdf-resource">
                    <xsl:with-param name="node" select="."/>
                </xsl:call-template>
            </div>
        </xsl:for-each>

    </xsl:template>

    <xsl:template name="anchor-rdf-resource">
        <xsl:param name="node"/>
        <xsl:param name="anchor-text"/>

        <xsl:variable name="text">
            <xsl:choose>
                <xsl:when test="$anchor-text">
                    <xsl:value-of select="$anchor-text"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$node/@rdf:resource"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="$node/@rdf:resource">
                <a href="{$node/@rdf:resource}"><xsl:value-of select="$text"/></a>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
