<?xml version="1.0" encoding="UTF-8"?>
<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:skos="http://www.w3.org/2004/02/skos/core#" xmlns:gemet="http://www.eionet.eu.int/GEMET/skos-ext#">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


<!-- match the root element ant set up the document -->

<xsl:template match="cds-thes">

	<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:gemet="http://www.eionet.eu.int/GEMET/skos-ext#" xml:base="http://www.eionet.eu.int/GEMET/">
	
		<skos:ConceptScheme rdf:about="http://www.eionet.eu.int/GEMET">
			<dc:title>General Multilingual Environment Thesaurus (GEMET)</dc:title>
		</skos:ConceptScheme>
		
		<xsl:apply-templates select="//super-group"/>
		<xsl:apply-templates select="//descriptor"/>
		<xsl:apply-templates select="//theme" mode="resource"/>
		<xsl:apply-templates select="//group" mode="resource"/>
		<xsl:apply-templates select="//descriptor-related"/>
	
	</rdf:RDF>
	
</xsl:template>


<!-- match each 'super-group' element -->
 
<xsl:template match="super-group">
	<xsl:variable name="id" select="@super-group-id"/>
	
	<gemet:SuperGroup rdf:about="sg_{$id}">
		<skos:inScheme rdf:resource="http://www.eionet.eu.int/GEMET"/>
	</gemet:SuperGroup>
	
</xsl:template>


<!-- match each 'descriptor' element -->

<xsl:template match="descriptor">
	<xsl:variable name="id" select="descriptor-term/@desc-id"/>
	
	<skos:Concept rdf:about="c_{$id}">
	
		<skos:inScheme rdf:resource="http://www.eionet.eu.int/GEMET"/>

		<xsl:apply-templates select="broader-term"/>
		<xsl:apply-templates select="narrower-term"/>
		<xsl:apply-templates select="theme" mode="relation"/>
		
		<xsl:if test="count(broader-term)=0">
			<xsl:apply-templates select="group" mode="relation"/>
		</xsl:if>
	
	</skos:Concept>

</xsl:template>


<!-- match each 'theme' element -->

<xsl:template match="theme" mode="resource">
	<xsl:variable name="id" select="@theme-id"/>

	<gemet:Theme rdf:about="t_{$id}">
		<skos:inScheme rdf:resource="http://www.eionet.eu.int/GEMET"/>
	</gemet:Theme>

</xsl:template>


<!-- match the 'theme' element to create a semantic relation -->

<xsl:template match="theme" mode="relation">
	<xsl:variable name="id" select="@theme-id"/>

	<gemet:broaderTheme rdf:resource="t_{$id}"/>
	
</xsl:template>


<!-- match each 'group' element -->

<xsl:template match="group" mode="resource">
	<xsl:variable name="id" select="@group-id"/>
	<xsl:variable name="sid" select="@super-group-id"/>

	<gemet:Group rdf:about="g_{$id}">
		<skos:inScheme rdf:resource="http://www.eionet.eu.int/GEMET"/>
		<gemet:subGroupOf rdf:resource="sg_{$sid}"/>
	</gemet:Group>

</xsl:template>


<!-- match the 'group' element to create a semantic relation -->

<xsl:template match="group" mode="relation">
	<xsl:variable name="id" select="@group-id"/>

	<gemet:broaderGroup rdf:resource="g_{$id}"/>

</xsl:template>


<!-- match the 'broader-term' element to create a semantic relation -->

<xsl:template match="broader-term">
	<xsl:variable name="id" select="@desc-ref-id"/>

	<skos:broader rdf:resource="c_{$id}"/>

</xsl:template>


<!-- match the 'narrower-term' element to create a semantic relation -->

<xsl:template match="narrower-term">
	<xsl:variable name="id" select="@desc-ref-id"/>

	<skos:narrower rdf:resource="c_{$id}"/>

</xsl:template>


<!-- match each 'descriptor-related' element to create a semantic relation -->

<xsl:template match="descriptor-related">
	<xsl:variable name="id" select="@desc-ref-id"/>
	<xsl:variable name="rid" select="@rel-desc-ref-id"/>

	<rdf:Description rdf:about="c_{$id}">
		<skos:related rdf:resource="c_{$rid}"/>	
	</rdf:Description>

</xsl:template>


</xsl:stylesheet>
