<?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:param name="Language"/>
	<xsl:output method="text" encoding="iso-8859-1"/>
	<xsl:template match="bs8723:Thesaurus">
		<xsl:variable name="MaxLenghtNotation">
			<xsl:call-template name="MaxLenghtNotation"/>
		</xsl:variable>
		<xsl:apply-templates select="bs8723:ThesaurusArray[count(bs8723:SuperOrdinate) = 0]">
			<xsl:with-param name="Indentation" select="number(0)"/>
			<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
			<xsl:with-param name="ParentNotation"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="bs8723:ThesaurusArray">
		<xsl:param name="Indentation"/>
		<xsl:param name="MaxLenghtNotation"/>
		<xsl:param name="ParentNotation"/>
		<xsl:variable name="CurrentNotation">
			<xsl:choose>
				<xsl:when test="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]">
					<xsl:value-of select="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:Notation"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$ParentNotation"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:apply-templates select="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]">
			<xsl:with-param name="Indentation" select="$Indentation"/>
			<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
		</xsl:apply-templates>
		<xsl:for-each select="bs8723:HasMember">
			<xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[@dc:identifier = current()]">
				<xsl:with-param name="Indentation" select="$Indentation"/>
				<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
				<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
			</xsl:apply-templates>
		</xsl:for-each>
		<xsl:for-each select="bs8723:HasMember">
			<xsl:if test="/bs8723:Thesaurus/bs8723:ThesaurusArray[@dc:identifier = current()]">
				<xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusArray[@dc:identifier = current()]">
					<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
					<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
					<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="bs8723:IsLabelledBy">
		<xsl:param name="Indentation"/>
		<xsl:param name="MaxLenghtNotation"/>
		<xsl:call-template name="Notation">
			<xsl:with-param name="Value" select="bs8723:Notation"/>
			<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
		</xsl:call-template>
		<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:text>&lt;</xsl:text>
		<xsl:value-of select="bs8723:LexicalValue"/>
		<xsl:text>&gt;</xsl:text>
		<xsl:text>
</xsl:text>
	</xsl:template>
	<xsl:template match="bs8723:ThesaurusConcept">
		<xsl:param name="Indentation"/>
		<xsl:param name="MaxLenghtNotation"/>
		<xsl:param name="ParentNotation"/>
		<xsl:variable name="CurrentNotation">
			<xsl:choose>
				<xsl:when test="count(bs8723:Notation) = 1">
					<xsl:value-of select="bs8723:Notation"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="bs8723:Notation">
						<xsl:choose>
							<xsl:when test="$ParentNotation != ''">
								<xsl:if test="starts-with(., $ParentNotation)">
									<xsl:value-of select="."/>
								</xsl:if>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="."/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:call-template name="Notation">
			<xsl:with-param name="Value" select="$CurrentNotation"/>
			<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
		</xsl:call-template>
		<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:if test="count(/bs8723:Thesaurus/bs8723:ThesaurusArray[bs8723:SuperOrdinate = current()/@dc:identifier]) &gt; 0">
			<xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusArray[bs8723:SuperOrdinate = current()/@dc:identifier]">
				<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
				<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
				<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
	<xsl:template name="Notation">
		<xsl:param name="MaxLenghtNotation"/>
		<xsl:param name="Value"/>
		<xsl:value-of select="$Value"/>
		<xsl:call-template name="Indentation">
			<xsl:with-param name="Indentation" select="number($MaxLenghtNotation) - string-length($Value)"/>
			<xsl:with-param name="IndentationString">
				<xsl:text> </xsl:text>
			</xsl:with-param>
		</xsl:call-template>
		<xsl:text>	</xsl:text>
	</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:template name="MaxLenghtNotation">
		<xsl:variable name="List">
			<xsl:for-each select="/bs8723:Thesaurus/bs8723:ThesaurusArray/bs8723:IsLabelledBy/bs8723:Notation|/bs8723:Thesaurus/bs8723:ThesaurusConcept/bs8723:Notation">
				<xsl:value-of select="string-length(.)"/>
				<xsl:text>-</xsl:text>
			</xsl:for-each>
		</xsl:variable>
		<xsl:call-template name="Max">
			<xsl:with-param name="List" select="$List"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template name="Max">
		<xsl:param name="List"/>
		<xsl:choose>
			<xsl:when test="$List = ''">
				<xsl:value-of select="number(0)"/>
			</xsl:when>
			<xsl:when test="$List = '-'">
				<xsl:value-of select="number(0)"/>
			</xsl:when>
			<xsl:when test="contains($List, '-')">
				<xsl:variable name="ThisValue" select="substring-before($List, '-')"/>
				<xsl:variable name="OtherValues">
					<xsl:call-template name="Max">
						<xsl:with-param name="List" select="substring($List, string-length($ThisValue) + 2)"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:choose>
					<xsl:when test="number($ThisValue) = number($OtherValues)">
						<xsl:value-of select="$ThisValue"/>
					</xsl:when>
					<xsl:when test="number($ThisValue) &gt; number($OtherValues)">
						<xsl:value-of select="$ThisValue"/>
					</xsl:when>
					<xsl:when test="number($ThisValue) &lt; number($OtherValues)">
						<xsl:value-of select="$OtherValues"/>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$List"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
