<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:bs8723="http://schema.bsi-global.com/BS8723-5/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns="http://www.w3.org/1999/xhtml">
	<xsl:output method="text" encoding="iso-8859-1"/>
	<xsl:param name="Language"/>
	<xsl:template match="bs8723:Thesaurus">
		<xsl:apply-templates select="bs8723:ThesaurusConcept[count(bs8723:HasBroaderConcept) = 0]">
			<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:with-param name="Indentation" select="number(0)"/>
			<xsl:with-param name="IsRoot">
				<xsl:text>true</xsl:text>
			</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="bs8723:ThesaurusConcept">
		<xsl:param name="Indentation"/>
		<xsl:param name="IsRoot"/>
		<xsl:if test="$IsRoot = 'true'">
			<xsl:text>
</xsl:text>
		</xsl:if>
		<xsl:call-template name="Indentation">
			<xsl:with-param name="Indentation" select="$Indentation"/>
			<xsl:with-param name="IndentationString">
				<xsl:text>.	</xsl:text>
			</xsl:with-param>
		</xsl:call-template>
		<xsl:value-of select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
		<xsl:text>
</xsl:text>
		<xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[@dc:identifier = current()/bs8723:HasNarrowerConcept]">
			<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
			<xsl:with-param name="IsRoot">
				<xsl:text>false</xsl:text>
			</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template name="Indentation">
		<xsl:param name="Indentation"/>
		<xsl:param name="IndentationString">.	</xsl:param>
		<xsl:choose>
			<xsl:when test="$Indentation = ''"/>
			<xsl:when test="number($Indentation) = number('not a number')"/>
			<xsl:when test="number($Indentation) &lt; 0"/>
			<xsl:when test="number($Indentation) = 0"/>
			<xsl:otherwise>
				<xsl:value-of select="$IndentationString"/>
				<xsl:call-template name="Indentation">
					<xsl:with-param name="Indentation" select="number($Indentation) - 1"/>
					<xsl:with-param name="IndentationString" select="$IndentationString"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
