<?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="thes">

	<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" 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#" xml:base="http://www.nla.gov.au/apais/thesaurus/">
	
		<skos:ConceptScheme rdf:about="http://www.nla.gov.au/apais/thesaurus">
			<dc:title>Australian Public Affairs Information Service (APAIS) thesaurus</dc:title>
		</skos:ConceptScheme>
		
		<xsl:apply-templates select="term"/>
	
	</rdf:RDF>
	
</xsl:template>


<!-- match each 'term' element -->
 
<xsl:template match="term">
	<xsl:variable name="id" select="termId"/>
	<xsl:variable name="type" select="termType"/>
	
	<xsl:if test="$type='PT'">
		
		<xsl:choose>

			<xsl:when test="count(relation[relationType='BT'])=0">
				<skos:TopConcept rdf:about="{$id}">
					<skos:inScheme rdf:resource="http://www.nla.gov.au/apais/thesaurus"/>
					<skos:prefLabel><xsl:value-of select="termName"/></skos:prefLabel>
					<xsl:apply-templates select="relation"/>
					<xsl:apply-templates select="termNote"/>
				</skos:TopConcept>
			</xsl:when>

			<xsl:otherwise>
				<skos:Concept rdf:about="{$id}">
					<skos:inScheme rdf:resource="http://www.nla.gov.au/apais/thesaurus"/>
					<skos:prefLabel><xsl:value-of select="termName"/></skos:prefLabel>
					<xsl:apply-templates select="relation"/>
					<xsl:apply-templates select="termNote"/>
				</skos:Concept>
			</xsl:otherwise>

		</xsl:choose>
			
	</xsl:if>
	
</xsl:template>


<!-- match each 'relation' element inside a preferred term -->

<xsl:template match="relation">
	<xsl:variable name="id" select="termId"/>
	<xsl:variable name="type" select="relationType"/>
	
	<xsl:if test="$type='UF'">
		<skos:altLabel><xsl:value-of select="termName"/></skos:altLabel>
	</xsl:if>

	<xsl:if test="$type='BT'">
		<skos:broader rdf:resource="{$id}"/>
	</xsl:if>

	<xsl:if test="$type='NT'">
		<skos:narrower rdf:resource="{$id}"/>
	</xsl:if>

	<xsl:if test="$type='RT'">
		<skos:related rdf:resource="{$id}"/>
	</xsl:if>

</xsl:template>



<xsl:template match="termNote">

	<skos:scopeNote><xsl:value-of select="."/></skos:scopeNote>
	
</xsl:template>

</xsl:stylesheet>
