<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns:inloc="http://purl.org/net/inloc/"
    version="1.0"
    exclude-result-prefixes="xsi xml inloc">
    <xsl:output 
        method="html" 
        encoding="utf-8" 
        doctype-system="about:legacy-compat"
        media-type="text/html" 
        omit-xml-declaration="yes" 
        indent="yes"/>
    <xsl:strip-space elements="*"/>
    
<xsl:variable name="baseuri" select="inloc:LOCstructure/@id"/>
<xsl:variable name="domainuri">
    <xsl:text>http://</xsl:text>
    <xsl:value-of select="substring-before(substring-after($baseuri,'http://'),'/')"/>    
    <xsl:text>/</xsl:text>
</xsl:variable>
    
<xsl:variable name="lasturipart">
    <xsl:call-template name="finduripath">
        <xsl:with-param name="uri" select="substring-after($baseuri,$domainuri)"></xsl:with-param>
    </xsl:call-template>            
</xsl:variable>

<xsl:variable name="locbaseuri">
    <xsl:value-of select="substring($baseuri,1,string-length($baseuri)-string-length($lasturipart))"/>
</xsl:variable>

<!--
    <xsl:value-of select="$baseuri"/>
    <xsl:text>
        </xsl:text>
    <xsl:value-of select="$domainuri"/>
    <xsl:text>
        </xsl:text>
    <xsl:value-of select="$lasturipart"/>
    <xsl:text>
        </xsl:text>
    <xsl:value-of select="$locbaseuri"/>
    <xsl:text>
        </xsl:text>
    -->
    <xsl:template match="inloc:LOCstructure">
<html>
        <head>
        <title><xsl:value-of select="inloc:title"/></title>
            <xsl:text disable-output-escaping="yes">
                &lt;style type="text/css"&gt;
    &lt;!--
@import url("http://purl.org/net/inloc/inloc-xml-to-html.css");
--&gt;
            &lt;/style&gt;
            </xsl:text>
        </head>
    <body>

<div title="LOCstructure">
    <table id="rounded-corner" summary="Main LOCstructure" width="100%">
        <thead>
            <tr>
                <th scope="col" width="20%" class="rounded-company">Metadata Name</th>
                <th scope="col" width="80%" class="rounded-q4">Metadata Value for root LOCstructure</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="2" class="rounded-foot-left"><em>This is the main LOCStructure, you will find below the related LOCassociation.</em></td>
                <td class="rounded-foot-right"> </td>
            </tr>
        </tfoot>
            

<!-- ********* LOCtypeLOC ************** -->
        <xsl:call-template name="LOCtypeLOC"/>

        <xsl:if test="inloc:combinationRules">
            <xsl:for-each select="inloc:combinationRules">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'combinationRules'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>

        </table>
</div>
        <xsl:variable name="structid" select="@id"/>

        <div title="LOCassociation">
        <xsl:for-each select="inloc:LOCassociation[inloc:subject/@id=$structid]">
            <xsl:call-template name="LOCassociation"/>
        </xsl:for-each>
        </div>
        <br/>
        
        <!-- ********* TEST LOCdefinition ************** -->
        <xsl:if test="inloc:LOCdefinition">
            <xsl:for-each select="inloc:LOCdefinition">
                <xsl:call-template name="LOCdefinition"/>
                <xsl:variable name="localid" select="@id"/>
                <div title="LOCassociation">
                    <xsl:for-each select="../inloc:LOCassociation[inloc:subject/@id=$localid]">
                    <xsl:call-template name="LOCassociation"/>
                </xsl:for-each>
                </div>
                <br/>
            </xsl:for-each>
        </xsl:if>
        
<!-- ********* TEST LOCassociation ************** -->        
        <xsl:if test="inloc:LOCassociation">
            <xsl:for-each select="inloc:LOCassociation">
                <xsl:if test="not(contains(inloc:subject/@id,$locbaseuri))">
                    <xsl:call-template name="LOCassociation"/>
                </xsl:if>
            </xsl:for-each>
        </xsl:if>
        
        <!-- ********* ADD GVIZ SCRIPT for Graphvisualisation ************** -->        
        <xsl:call-template name="GViz"/>
    </body>
</html>
    </xsl:template>

<!-- ********* TEMPLATE LOCtypeLOC ************** -->
    <xsl:template name="LOCtypeLOC">
    <xsl:if test="@id">
        <tr>
            <td>id</td>
            <td>
                <xsl:element name="a">
                    <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@id,$locbaseuri)"/>    
                    </xsl:attribute>
                    <xsl:attribute name="href">
                        <xsl:value-of select="@id"/>    
                    </xsl:attribute>
                    <xsl:value-of select="@id"/>
                </xsl:element>
            </td>
        </tr>
    </xsl:if>
    <xsl:if test="@xml:lang">
        <tr>
            <td>lang</td>
            <td><xsl:value-of select="@xml:lang"/></td>
        </tr>
    </xsl:if>
    <xsl:if test="inloc:extraID">
            <xsl:for-each select="inloc:extraID">
                <tr>
                    <td>extraID</td>
                    <td><xsl:value-of select="."/></td>
                </tr>
            </xsl:for-each>
        </xsl:if>
        
        <xsl:if test="inloc:title">
            <xsl:for-each select="inloc:title">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'title'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>

        <xsl:if test="inloc:abbr">
            <xsl:for-each select="inloc:abbr">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'abbr'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
        
        <xsl:if test="inloc:description">
            <xsl:for-each select="inloc:description">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'description'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>

        <xsl:if test="inloc:furtherInformation">
            <xsl:for-each select="inloc:furtherInformation">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'furtherInformation'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
        
        <xsl:if test="inloc:rights">
            <xsl:for-each select="inloc:rights">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'rights'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
        
        <xsl:if test="inloc:created">
            <tr>
                <td>created</td>
                <td><xsl:value-of select="inloc:created"/></td>
            </tr>
        </xsl:if>

        <xsl:if test="inloc:modified">
            <xsl:for-each select="inloc:modified">
                <tr>
                    <td>modified</td>
                    <td><xsl:value-of select="."/></td>
                </tr>
            </xsl:for-each>
        </xsl:if>
        
        <xsl:if test="inloc:issued">
            <tr>
                <td>issued</td>
                <td><xsl:value-of select="inloc:issued"/></td>
            </tr>
        </xsl:if>

        <xsl:if test="inloc:validityStart">
            <tr>
                <td>validityStart</td>
                <td><xsl:value-of select="inloc:validityStart"/></td>
            </tr>
        </xsl:if>

        <xsl:if test="inloc:validityEnd">
            <tr>
                <td>validityEnd</td>
                <td><xsl:value-of select="inloc:validityEnd"/></td>
            </tr>
        </xsl:if>

        <xsl:if test="inloc:version">
            <tr>
                <td>version</td>
                <td><xsl:value-of select="inloc:version"/></td>
            </tr>
        </xsl:if>
    </xsl:template>

<!-- ******************* LOCdefinition ************ -->
    <xsl:template name="LOCdefinition">

        <div title="LOCdefinition">
            
            <table id="rounded-corner" summary="LOCdefinition" width="100%">
                <thead>
                    <tr>
                        <th scope="col" width="20%" class="rounded-company">Metadata Name</th>
                        <th scope="col" width="80%" class="rounded-q4">Metadata Value for LOCdefinition</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="2" class="rounded-foot-left">
                            <em>This is a LOCdefinition, you will find below the related LOCassociation.
                                <xsl:if test="inloc:title"> LinkedIn Search: 
                                        <xsl:variable name="linkedinhref">
                                            <xsl:text disable-output-escaping="yes">http://www.linkedin.com/skills/skill/</xsl:text>
                                            <xsl:value-of select="inloc:title"/>
                                            <xsl:text disable-output-escaping="yes">?trk=skills-pg-search</xsl:text>
                                        </xsl:variable>
                                        <xsl:element name="a">
                                            <xsl:attribute name="href"><xsl:value-of select="$linkedinhref"/></xsl:attribute>
                                            <xsl:attribute name="title">Search in LinkedIn for <xsl:value-of select="inloc:title"/></xsl:attribute>
                                            <img src="linkedinsearch.jpg" width="5%" height="5%" border="0" alt="Search this LOCdefinition in LinkedIn Skills service"/>
                                            <!--            <xsl:text>Search this LOCdefinition in LinkedIn Skills service</xsl:text>-->
                                        </xsl:element>
                                        <!--
                <xsl:element name="a">
                <xsl:attribute name="href"><xsl:value-of select="'http://localhost/api/search?'+inloc:title"/></xsl:attribute>
                <img src="rapasso.jpg" border="0" alt="Search this LOCdefinition in Rapasso service"/>
            </xsl:element>
-->
                                </xsl:if>
                            </em>
                        </td>
                        <td class="rounded-foot-right"> </td>
                    </tr>
                </tfoot>
        
        <xsl:call-template name="LOCtypeLOC"/>
        
        <xsl:if test="inloc:primaryStructure">
            <tr>
                <td>primaryStructure</td>
                <td>
                    <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:choose>
                            <xsl:when test="substring-after(inloc:primaryStructure,$locbaseuri)!=''">
                                <xsl:text>#</xsl:text>
                                <xsl:value-of select="substring-after(inloc:primaryStructure,$locbaseuri)"/>    
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="inloc:primaryStructure"/>    
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:value-of select="inloc:primaryStructure"/>
                </xsl:element>
                </td>
            </tr>
        </xsl:if>
</table>        
        </div>
    </xsl:template>

<!-- ******************* LOCassociation ************ -->
    <xsl:template name="LOCassociation">

            <table id="box-table-a" summary="LOCassociation" width="75%">
                <thead>
                    <tr>
                        <th scope="col" width="20%">Metadata Name</th>
                        <th scope="col" width="80%">Metadata Value for LOCassociation</th>
                    </tr>
                </thead>
                <tbody>
            
        <xsl:if test="@id">
            <tr>
                <td>@id</td>
                <td><xsl:value-of select="@id"/></td>
            </tr>
        </xsl:if>
        
        <xsl:if test="@type!=''">
            <tr>
                <td>@type</td>
                <td><xsl:element name="a">
                    <xsl:attribute name="href"><xsl:value-of select="@type"/></xsl:attribute>
                    <xsl:value-of select="@type"/>
                </xsl:element></td>
            </tr>
        </xsl:if>
        
        <xsl:if test="inloc:subject">
            <tr>
                <td>subject id</td>
                <td>
                    <xsl:element name="a">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="substring-after(inloc:subject/@id,$locbaseuri)!=''">
                                    <xsl:text>#</xsl:text>
                                    <xsl:value-of select="substring-after(inloc:subject/@id,$locbaseuri)"/>    
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="inloc:subject/@id"/>    
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                        <xsl:value-of select="inloc:subject/@id"/>
                    </xsl:element>
                </td>
            </tr>
                <xsl:for-each select="inloc:subject/inloc:label">
                    <xsl:call-template name="multilingualString">
                        <xsl:with-param name="type" select="'subject label'"/>
                    </xsl:call-template>
                </xsl:for-each>
        </xsl:if>

        <xsl:if test="inloc:scheme">
            <tr>
                <td>scheme id</td>
                <td>
                    <xsl:element name="a">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="substring-after(inloc:scheme/@id,$locbaseuri)!=''">
                                    <xsl:text>#</xsl:text>
                                    <xsl:value-of select="substring-after(inloc:scheme/@id,$locbaseuri)"/>    
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="inloc:scheme/@id"/>    
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                        <xsl:value-of select="inloc:scheme/@id"/>
                    </xsl:element>
                </td>
            </tr>
            <xsl:for-each select="inloc:scheme/inloc:label">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'scheme label'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>

        <xsl:if test="inloc:object">
            <tr>
                <td>object id</td>
                <td>
                    <xsl:element name="a">
                        <xsl:attribute name="href">
                            <xsl:choose>
                                <xsl:when test="substring-after(inloc:object/@id,$locbaseuri)!=''">
                                    <xsl:text>#</xsl:text>
                                    <xsl:value-of select="substring-after(inloc:object/@id,$locbaseuri)"/>    
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="inloc:object/@id"/>    
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                        <xsl:value-of select="inloc:object/@id"/>
                    </xsl:element>
                </td>
            </tr>
            <xsl:for-each select="inloc:object/inloc:label">
                <xsl:call-template name="multilingualString">
                    <xsl:with-param name="type" select="'object label'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>

        <xsl:if test="inloc:number!=''">
            <tr>
                <td>number</td>
                <td><xsl:value-of select="inloc:number"/></td>
            </tr>
        </xsl:if>

                    </tbody>
            </table>   
    </xsl:template>

    <!-- ******************* MULTILINGUAL STRING ************ -->
    <xsl:template name="multilingualString">
        <xsl:param name="type"/>
        <xsl:choose>
            <xsl:when test="@xml:lang">
                <tr>
                    <td><xsl:value-of select="$type"/> (<xsl:value-of select="@xml:lang"/>)</td>
                    <td><xsl:value-of select="."/></td>
                </tr>
            </xsl:when>
            <xsl:otherwise>
                <tr>
                    <td><xsl:value-of select="$type"/></td>
                    <td><xsl:value-of select="."/></td>
                </tr>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <!-- ******************* MULTILINGUAL STRING ************ -->
    <xsl:template name="finduripath">
        <xsl:param name="uri"/>
        <xsl:choose>
            <xsl:when test="substring-after($uri,'/')!=''">
<xsl:call-template name="finduripath">
    <xsl:with-param name="uri" select="substring-after($uri,'/')"></xsl:with-param>
</xsl:call-template>            
            </xsl:when>
            <xsl:otherwise>
<xsl:value-of select="$uri"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="GViz">
        <xsl:text disable-output-escaping="yes">
        &lt;script type="text/javascript" src="http://mvancoillie.free.fr/inloc-demo/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
        &lt;script type="text/javascript" src="http://gviz.oodavid.com/gviz.js"&gt;&lt;/script&gt;
        &lt;script type="gviz" data-layout="fdp"&gt;&lt;![CDATA[
    digraph G {
        // General Settings
        graph [
            truecolor=true
            bgcolor="#FFFFFF00"
            fontname = "Ubuntu"
            size = "15"
        ];
        node [
            shape=ellipse
            style=filled
            fillcolor="#C1CDCD"
            color=transparent
            fontcolor="#0F192A"
        ];
        edge [
            color="#0F192A"
            fontname="arial"
            fontsize="12"
        ];
        // Define our Nodes
        </xsl:text>
        <xsl:for-each select="inloc:LOCdefinition">
            <xsl:choose>
                <xsl:when test="substring-after(@id,$locbaseuri)!=''">
                    <xsl:value-of select="translate(substring-after(@id,$locbaseuri),'-_./#','')"/>    
                </xsl:when>
                <xsl:when test="substring-after(@id,$locbaseuri)=''">
                    <xsl:value-of select="translate($lasturipart,'-_./#','')"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text> [label="</xsl:text>
            <xsl:choose>
                <xsl:when test="inloc:abbr">
                    <xsl:value-of select="inloc:abbr"/>
                </xsl:when>
                <xsl:when test="inloc:title">
                    <xsl:value-of select="inloc:title"/>
                </xsl:when>
                <xsl:when test="description">
                    <xsl:value-of select="substring(inloc:description,1, 20)"/>
                </xsl:when>
                <xsl:when test="@id">
                    <xsl:choose>
                        <xsl:when test="substring-after(@id,$locbaseuri)!=''">
                            <xsl:value-of select="translate(substring-after(@id,$locbaseuri),'-_./#','')"/>    
                        </xsl:when>
                        <xsl:when test="substring-after(@id,$locbaseuri)=''">
                            <xsl:value-of select="translate($lasturipart,'-_./#','')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="@id"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
            </xsl:choose>
            <xsl:text>"];
</xsl:text>
        </xsl:for-each>
        <xsl:text>        // Define our Connections
</xsl:text>
        <xsl:for-each select="inloc:LOCassociation[@type='http://purl.org/net/inloc/LOCrel']">
            <xsl:choose>
                <xsl:when test="substring-after(inloc:subject/@id,$locbaseuri)!=''">
                    <xsl:value-of select="translate(substring-after(inloc:subject/@id,$locbaseuri),'-_./#','')"/>    
                </xsl:when>
                <xsl:when test="substring-after(inloc:subject/@id,$locbaseuri)=''">
                    <xsl:value-of select="translate($lasturipart,'-_./#','')"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text disable-output-escaping="yes"> -> </xsl:text>
            <xsl:choose>
                <xsl:when test="substring-after(inloc:object/@id,$locbaseuri)!=''">
                    <xsl:value-of select="translate(substring-after(inloc:object/@id,$locbaseuri),'-_./#','')"/>    
                </xsl:when>
                <xsl:when test="substring-after(inloc:object/@id,$locbaseuri)=''">
                    <xsl:value-of select="translate($lasturipart,'-_./#','')"/>
                </xsl:when>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="contains(inloc:scheme/@id,'http://purl.org/net/inloc/')">
                    <xsl:text> [label="</xsl:text>
                    <xsl:value-of select="substring-after(inloc:scheme/@id,'http://purl.org/net/inloc/')"/>
                    <xsl:text>"];
</xsl:text>
                </xsl:when>
                <xsl:when test="inloc:scheme/inloc:label">
                    <xsl:text> [label="</xsl:text>
                    <xsl:value-of select="inloc:scheme/inloc:label"/>
                    <xsl:text>"];
</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>;
</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>            
        <xsl:text disable-output-escaping="yes">    }
]]&gt;&lt;/script&gt;
</xsl:text>
    </xsl:template>
    
</xsl:stylesheet>