<?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:output method="text" encoding="iso-8859-1"/>
	<xsl:param name="Language"/>
	<xsl:include href="DD8723-5Utility.xsl"/>
	<xsl:key name="narrower-arrays" match="bs8723:ThesaurusArray[bs8723:IsLabelledBy]" use="bs8723:HasSuperOrdinate"/>
	<xsl:key name="narrower-concepts" match="bs8723:ThesaurusConcept" use="bs8723:HasHierRelConcept[starts-with(@Role, 'BT')]"/>
	<xsl:key name="concept" match="bs8723:ThesaurusConcept" use="@dc:identifier"/>
	<xsl:key name="array" match="bs8723:ThesaurusArray[bs8723:IsLabelledBy]" use="@dc:identifier"/>

	<xsl:template match="bs8723:Thesaurus">
		<xsl:apply-templates select="bs8723:ThesaurusConcept[not(bs8723:HasHierRelConcept[starts-with(@Role, 'BT')])]">
			<xsl:sort select="bs8723:PreferredTerm[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:with-param name="Indentation" select="number(0)"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="bs8723:ThesaurusConcept">
		<xsl:param name="Indentation"/>
		<xsl:param name="ParentConcept"/>
		<xsl:variable name="CurrentConcept" select="."/>
		<xsl:if test="number($Indentation) = 0">
			<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: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="key('narrower-arrays', @dc:identifier)"/>
		<xsl:variable name="Concepts" select="key('narrower-concepts', @dc:identifier)"/>
		<!--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="$Arrays"/>
					<xsl:with-param name="ConceptID" select="@dc:identifier"/>
				</xsl:call-template>
			</xsl:variable>
			<!-- sort statement added by rbl -->
			<xsl:if test="not(contains($Contains, 'true'))">
				<xsl:apply-templates select=".">
					<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
					<xsl:with-param name="ParentConcept" select="$CurrentConcept"/>
					<xsl:sort select="bs8723:PreferredTerm/bs8723:LexicalValue/text()"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:for-each>
		<!--Show node labels characteristic of division-->
		<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="Concepts" select="$Concepts"/>
			<xsl:with-param name="ParentConcept" select="$CurrentConcept"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="bs8723:ThesaurusArray">
		<xsl:param name="Indentation"/>
		<xsl:param name="Concepts"/>
		<xsl:param name="ParentConcept"/>
		<xsl:variable name="Contains">
			<xsl:call-template name="ArrayContainsConcepts">
				<xsl:with-param name="ArrayID" select="@dc:identifier"/>
				<xsl:with-param name="Concepts" select="$Concepts"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:if test="contains($Contains, 'true')">
			<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:IsLabelledBy[not(@xml:lang) or (@xml:lang = $Language)]/bs8723:LexicalValue"/>
			<xsl:text>&gt;</xsl:text>
			<xsl:text>
</xsl:text>
			<xsl:choose>
				<xsl:when test="@Ordered = 'true'">
					<xsl:for-each select="bs8723:HasMember">
						<!--xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[@dc:identifier = current()]"-->
						<xsl:apply-templates select="key('concept', .)">
							<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
							<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
						</xsl:apply-templates>
					</xsl:for-each>
					<xsl:for-each select="bs8723:HasMember">
						<!--xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusArray[(@dc:identifier = current()) and (bs8723:IsLabelledBy)]"-->
						<xsl:apply-templates select="key('array', .)">
							<xsl:with-param name="Indentation" select="number($Indentation) + 1"/>
							<xsl:with-param name="Concepts" select="$Concepts"/>
							<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
						</xsl:apply-templates>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<!--xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusConcept[@dc:identifier = current()/bs8723:HasMember]"-->
					<xsl:apply-templates select="key('concept', bs8723:HasMember)">
						<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="ParentConcept" select="$ParentConcept"/>
					</xsl:apply-templates>
					<!--xsl:apply-templates select="/bs8723:Thesaurus/bs8723:ThesaurusArray[(@dc:identifier = current()/bs8723:HasMember) and (bs8723:IsLabelledBy)]"-->
					<xsl:apply-templates select="key('array', bs8723:HasMember)">
						<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="Concepts" select="$Concepts"/>
						<xsl:with-param name="ParentConcept" select="$ParentConcept"/>
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>
