<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" encoding="iso-8859-1"/>
	<xsl:param name="Language"/>
	<xsl:key name="narrow-term" match="term[termType='PT']" use="relation[relationType = 'BT']/termName"/>
	<xsl:key name="narrow-node-label" match="term[termType='NL']" use="relation[relationType = 'BT']/termName"/>

	<xsl:template name="CheckForTermInNodeLabel">
		<xsl:param name="term"/>
		<xsl:param name="node-labels"/>				
		<xsl:for-each select="$node-labels">
			<xsl:if test="relation[relationType='NT' and termName=$term]"><xsl:text>yes</xsl:text></xsl:if>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="Zthes">
		<xsl:apply-templates select="term[(not(termLanguage) or (termLanguage = $Language)) and not(relation[relationType = 'BT']) and (termType = 'PT')]">
			<xsl:sort select="translate(termName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
			<xsl:with-param name="Indentation">
				<xsl:text>0</xsl:text>
			</xsl:with-param>
			<xsl:with-param name="IsRoot">
				<xsl:text>true</xsl:text>
			</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="term">
		<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:choose>
			<xsl:when test="termType = 'NL'">	
				<xsl:text>&lt;</xsl:text>
				<xsl:value-of select="termName"/>
				<xsl:text>&gt;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="termName"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:text>
</xsl:text>
		<!-- added separate processing for PTs and NLs; filter out NTs which also occur within NLs: rbl -->
		<xsl:variable name="narrow-terms" select="key('narrow-term', termName)[not(termLanguage) or (termLanguage = $Language)]"/>
		<xsl:variable name="current-term" select="termName"/>
		<xsl:for-each select="$narrow-terms">
			<xsl:sort select="translate(termName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
			<xsl:variable name="FilterThisTerm">
				<xsl:call-template name="CheckForTermInNodeLabel">
					<xsl:with-param name="term" select="termName"/>
					<xsl:with-param name="node-labels" select="key('narrow-node-label', $current-term)"/>				
				</xsl:call-template>
			</xsl:variable>
			<xsl:if test="$FilterThisTerm=''">
				<xsl:apply-templates select=".">
					<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:if>
		</xsl:for-each>
		<!--xsl:apply-templates select="/Zthes/term[termType='PT'][(not(termLanguage) or (termLanguage = $Language)) and (relation[(relationType = 'BT') and (termName = current()/termName)])]">
			<xsl:sort select="translate(termName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
			<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:apply-templates select="/Zthes/term[termType='NL'][(not(termLanguage) or (termLanguage = $Language)) and (relation[(relationType = 'BT') and (termName = current()/termName)])]"-->
		<xsl:apply-templates select="key('narrow-node-label', termName)[not(termLanguage) or (termLanguage = $Language)]">
			<xsl:sort select="translate(termName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
			<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>
