<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:bs8723="http://www.bsigroup.com/resources/standards/bs8723/"
	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:include href="DD8723-5Utility.xsl"/>
	<xsl:template match="bs8723:Thesaurus">
		<xsl:variable name="MaxLenghtNotation">
			<xsl:call-template name="MaxLenghtNotation"/>
		</xsl:variable>
		<xsl:variable name="RootArrays" select="/bs8723:Thesaurus/bs8723:ThesaurusArray[not(bs8723:HasSuperOrdinate) and (bs8723:IsLabelledBy)]"/>
		<xsl:variable name="AllArrays" select="/bs8723:Thesaurus/bs8723:ThesaurusArray[bs8723:IsLabelledBy]"/>
		<xsl:variable name="Concepts" select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[not(bs8723:HasHierRelConcept[starts-with(@Role, 'BT')])]"/>
		<!--Show normal narrower terms-->
		<xsl:for-each select="$Concepts/.">
			<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:variable name="Contains">
				<xsl:call-template name="ArraysContainsConcept">
					<xsl:with-param name="Arrays" select="$AllArrays"/>
					<xsl:with-param name="ConceptID" select="@dc:identifier"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="not(contains($Contains, 'true'))">
				<xsl:apply-templates select=".">
					<xsl:with-param name="Indentation" select="number(0)"/>
					<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
					<xsl:with-param name="ParentNotation"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:for-each>
		<!--Show node labels caracteristic of division-->
		<xsl:apply-templates select="$RootArrays/.">
			<xsl:sort select="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<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:param name="ParentConcept"/>
		<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:variable name="Arrays" select="/bs8723:Thesaurus/bs8723:ThesaurusArray[(@dc:identifier= current()/bs8723:HasMember) and (bs8723:IsLabelledBy)]"/>
		<xsl:variable name="Concepts" select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[@dc:identifier = current()/bs8723:HasMember]"/>
		<xsl:choose>
			<xsl:when test="@Ordered = 'true'">
				<xsl:for-each select="$Concepts/.">
					<xsl:variable name="Contains">
						<xsl:call-template name="ArraysContainsConcept">
							<xsl:with-param name="Arrays" select="$Arrays"/>
							<xsl:with-param name="ConceptID" select="@dc:identifier"/>
						</xsl:call-template>
					</xsl:variable>
					<xsl:if test="not(contains($Contains, 'true'))">
						<xsl:apply-templates select=".">
							<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
							<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
							<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
							<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
						</xsl:apply-templates>
					</xsl:if>
				</xsl:for-each>
				<xsl:apply-templates select="$Arrays/.">
					<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
					<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
					<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
					<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:for-each select="$Concepts/.">
					<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
					<xsl:variable name="Contains">
						<xsl:call-template name="ArraysContainsConcept">
							<xsl:with-param name="Arrays" select="$Arrays"/>
							<xsl:with-param name="ConceptID" select="@dc:identifier"/>
							<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
						</xsl:call-template>
					</xsl:variable>
					<xsl:if test="not(contains($Contains, 'true'))">
						<xsl:apply-templates select=".">
							<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
							<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
							<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
							<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
						</xsl:apply-templates>
					</xsl:if>
				</xsl:for-each>
				<xsl:apply-templates select="$Arrays/.">
					<xsl:sort select="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
					<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
					<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
					<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
					<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</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:param name="ParentConcept"/>
		<xsl:variable name="CurrentConcept" select="."/>
		<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:if test="$ParentConcept">
			<xsl:variable name="Role" select="$ParentConcept/bs8723:HasHierRelConcept[(starts-with(@Role, 'NT')) and (. = current()/@dc:identifier)]/@Role"/>
			<xsl:variable name="UserDefinedRole" select="substring-after($Role, '/')"/>
			<xsl:if test="string-length($UserDefinedRole) &gt; 0">
				<xsl:value-of select="$UserDefinedRole"/>
				<xsl:text>: </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:value-of select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
		<xsl:text>
</xsl:text>
		<xsl:variable name="Arrays" select="/bs8723:Thesaurus/bs8723:ThesaurusArray[(bs8723:HasSuperOrdinate = current()/@dc:identifier) and (bs8723:IsLabelledBy)]"/>
		<xsl:variable name="Concepts" select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[bs8723:HasHierRelConcept[starts-with(@Role, 'BT')] = current()/@dc:identifier]"/>
		<xsl:for-each select="$Concepts/.">
			<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:variable name="Contains">
				<xsl:call-template name="ArraysContainsConcept">
					<xsl:with-param name="Arrays" select="$Arrays"/>
					<xsl:with-param name="ConceptID" select="@dc:identifier"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="not(contains($Contains, 'true'))">
				<xsl:apply-templates select=".">
					<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
					<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
					<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
					<xsl:with-param name="ParentConcept" select="$CurrentConcept"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:for-each>
		<xsl:apply-templates select="$Arrays/.">
			<xsl:sort select="bs8723:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
			<xsl:with-param name="MaxLenghtNotation" select="$MaxLenghtNotation"/>
			<xsl:with-param name="ParentNotation" select="$CurrentNotation"/>
			<xsl:with-param name="ParentConcept" select="$CurrentConcept"/>
		</xsl:apply-templates>
	</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="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>
