<?xml version='1.0' encoding="utf-8"?>

<!-- thoML to XTM transformer version v0.1 -->
<!-- changes to v0.0:                      -->
<!--  + added views (see occurrence)       -->
<!--  + moved functional schema to proxy   -->
<xsl:transform	version="1.0"
				xmlns="http://www.topicmaps.org/xtm/1.0/"
 				xmlns:xtm="http://www.topicmaps.org/xtm/1.0/"
				xmlns:tml="http://www.lexit.at/thoML"
				xmlns:xlink="http://www.w3.org/1999/xlink"
	 			xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	 			exclude-result-prefixes = "tml"
>

	<!-- Generate a legible, compact XTM file -->
	<xsl:output method="xml" encoding="ISO-8859-1" indent="yes" omit-xml-declaration="no" />
	<xsl:strip-space elements="*"/>


	<!-- Use a lookup table for declared items tho/declaration/item  -->
	<xsl:key name="decItemKey" match="tml:item" use="@key"/>


	<!-- The tho-container element gets transformed to the topicMap-container element -->
	<xsl:template match="tml:tho">
		<topicMap>
			<xsl:attribute name="id">
  				<xsl:value-of select="@id"/><xsl:text>.xtm</xsl:text>
  			</xsl:attribute>
  			<xsl:apply-templates />
		</topicMap>
	</xsl:template>


	<!-- The about element gets transformed into a metainfo-topic element -->
	<xsl:template match="tml:about">
		<topic>
			<xsl:attribute name="id">
  				<xsl:value-of select="//tml:tho/@id"/><xsl:text>.xtm-metainfo</xsl:text>
  			</xsl:attribute>
			<subjectIdentity>
  				<subjectIndicatorRef>
  					<xsl:attribute name="xlink:href">
  						<xsl:text>#</xsl:text><xsl:value-of select="//tml:tho/@id"/><xsl:text>.xtm</xsl:text>
  					</xsl:attribute>
  				</subjectIndicatorRef>
  			</subjectIdentity>
			<baseName>
  				<baseNameString><xsl:value-of select="." /></baseNameString>
  			</baseName>
  		</topic>
	</xsl:template>

	<!-- The tho-type-definition element is omitted in the output tree -->
	<xsl:template match="tml:tho-type-definition">
	</xsl:template>

	<!-- The declaration element is omitted in the output tree -->
	<xsl:template match="tml:declaration">
	</xsl:template>

	<!-- The thread element gets transformed into a topic element-->
	<xsl:template match="tml:thread">
    <xsl:param name="currentView" select="tml:view" />
		<topic>
			<xsl:attribute name="id">
  				<xsl:value-of select="@id" />
  			</xsl:attribute>
			<instanceOf>
				<topicRef>
					<xsl:attribute name="xlink:href">
						<xsl:value-of select="key('decItemKey',//tml:thread-type)"/>
					</xsl:attribute>
				</topicRef>
  			</instanceOf>
  			<baseName>
  				<baseNameString>
            <xsl:value-of select="@name"/>
            <xsl:if test="tml:view">
              (aus Sichtweise: <xsl:value-of select="document('../threadbase/fhstg_ontologie.xtm')//xtm:topic[@id=$currentView]/xtm:baseName/xtm:baseNameString"/>)
            </xsl:if>
          </baseNameString>
  			</baseName>
  			<!-- Transform all docbase-mapping subelements-->
			<xsl:apply-templates select="tml:docbase-mapping"/>
		</topic>
		<!-- Transform all participant subelements-->
		<xsl:apply-templates select="tml:participant"/>
		<!-- Transform all subthread subelements-->
		<xsl:apply-templates select="tml:subthread"/>
	</xsl:template>



	<!-- The participant element gets transformed into an association element:           -->
	<!--                                                                                 -->
	<!-- <participant>ELEMENT</participant>        transformed to                        -->
	<!--                                                                                 -->
	<!-- <association id="$threadID.tcpXX">                                              -->
	<!--  <instanceOf>                                                                   -->
	<!--   <topicRef xlink:href="$relation-participant-type" />                          -->
	<!--  </instanceOf>                                                                  -->
	<!--  <member>                                                                       -->
	<!--   <roleSpec>                                                                    -->
	<!--    <topicRef xlink:href="$thread-type" />                                       -->
	<!--   </roleSpec>                                                                   -->
	<!--   <topicRef xlink:href="$threadID" />                                           -->
	<!--  </member>                                                                      -->
	<!--  <member>                                                                       -->
	<!--   <roleSpec>                                                                    -->
	<!--    <topicRef xlink:href="$participant-type" />                                  -->
	<!--   </roleSpec>                                                                   -->
	<!--   <topicRef xlink:href="$ELEMENT" />                                            -->
	<!--  </member>                                                                      -->
	<!-- </association>                                                                  -->
	<xsl:template match="tml:participant">
		<association>
				<!-- The id of association is threadID.tcaXX -->
				<xsl:attribute name="id">
  					<xsl:value-of select="../@id" /><xsl:text>.tcp</xsl:text><xsl:value-of select="position()-1"/>
  				</xsl:attribute>
			<instanceOf>
				<!-- The type of association is taken from the tho-type-definition -->
				<topicRef>
  					<xsl:attribute name="xlink:href">
  						<xsl:value-of select="key('decItemKey',//tml:relation-participant-type)"/>
  					</xsl:attribute>
  				</topicRef>
  			</instanceOf>
			<member>
				<roleSpec>
					<!-- The type of the thread members role is taken from the tho-type-definition -->
  					<topicRef>
  						<xsl:attribute name="xlink:href">
  							<xsl:value-of select="key('decItemKey',//tml:thread-type)"/>
  						</xsl:attribute>
  					</topicRef>
  				</roleSpec>
  				<!-- The reference to the thread we are talking about -->
  				<topicRef>
  					<xsl:attribute name="xlink:href">
  						<xsl:text>#</xsl:text><xsl:value-of select="../@id"/>
  					</xsl:attribute>
  				</topicRef>
  			</member>
			<member>
				<roleSpec>
					<!-- The type of the participant members role is taken from the tho-type-definition -->
  					<topicRef>
  						<xsl:attribute name="xlink:href">
  							<xsl:value-of select="key('decItemKey',//tml:participant-type)"/>
  						</xsl:attribute>
  					</topicRef>
  				</roleSpec>
  					<topicRef>
  					<!-- The value of the participant we are talking about -->
  					<xsl:attribute name="xlink:href">
  						<xsl:value-of select="key('decItemKey', . )"/>
  					</xsl:attribute>
  				</topicRef>
  			</member>
  		</association>
	</xsl:template>



	<!-- The subthread element gets transformed into an association element:          -->
	<!--                                                                              -->
	<!-- <subthread>THREAD</subthread>        transformed to                          -->
	<!--                                                                              -->
	<!-- <association id="$threadID.tcsXX">                                           -->
	<!--  <instanceOf>                                                                -->
	<!--   <topicRef xlink:href="$relation-subthread-type" />                         -->
	<!--  </instanceOf>                                                               -->
	<!--  <member>                                                                    -->
	<!--   <roleSpec>                                                                 -->
	<!--    <topicRef xlink:href="$thread-type" />                                    -->
	<!--   </roleSpec>                                                                -->
	<!--   <topicRef xlink:href="$threadID" />                                        -->
	<!--  </member>                                                                   -->
	<!--  <member>                                                                    -->
	<!--   <roleSpec>                                                                 -->
	<!--    <topicRef xlink:href="$subthread-type" />                                 -->
	<!--   </roleSpec>                                                                -->
	<!--   <topicRef xlink:href="$THREAD" />                                          -->
	<!--  </member>                                                                   -->
	<!-- </association>                                                               -->
	<xsl:template match="tml:subthread">
		<xsl:if test="normalize-space(.)">
		<association>
				<!-- The id of association is threadID.tcsXX -->
				<xsl:attribute name="id">
  					<xsl:value-of select="../@id" /><xsl:text>.tcs</xsl:text><xsl:value-of select="position()-1"/>
  				</xsl:attribute>
			<instanceOf>
				<!-- The type of association is taken from the tho-type-definition -->
				<topicRef>
  					<xsl:attribute name="xlink:href">
  						<xsl:value-of select="key('decItemKey',//tml:relation-subthread-type)"/>
  					</xsl:attribute>
  				</topicRef>
  			</instanceOf>
			<member>
				<roleSpec>
					<!-- The type of the thread members role is taken from the tho-type-definition -->
  					<topicRef>
  						<xsl:attribute name="xlink:href">
  							<xsl:value-of select="key('decItemKey',//tml:thread-type)"/>
  						</xsl:attribute>
  					</topicRef>
  				</roleSpec>
  				<!-- The reference to the thread we are talking about -->
  				<topicRef>
  					<xsl:attribute name="xlink:href">
  						<xsl:text>#</xsl:text><xsl:value-of select="../@id"/>
  					</xsl:attribute>
  				</topicRef>
  			</member>
			<member>
				<roleSpec>
					<!-- The type of the subthread members role is taken from the tho-type-definition -->
  					<topicRef>
  						<xsl:attribute name="xlink:href">
  							<xsl:value-of select="key('decItemKey',//tml:subthread-type)"/>
  						</xsl:attribute>
  					</topicRef>
  				</roleSpec>
  					<topicRef>
  					<!-- The reference to the subthread we are talking about -->
  					<xsl:attribute name="xlink:href">
  						<xsl:text>#</xsl:text><xsl:value-of select="."/>
  					</xsl:attribute>
  				</topicRef>
  			</member>
  		</association>
  		</xsl:if>
	</xsl:template>



	<!-- The docbase-mapping element gets transformed into an occurrence element:     -->
	<!--                                                                              -->
	<!-- <docbase-mapping>                                                            -->
	<!--  URI-OF-ITEM                                                                 -->
	<!-- </docbase-mapping>    transformed to                                         -->
	<!--                                                                              -->
	<!-- <occurrence>                                                                 -->
	<!--  <scope>                                                                     -->
	<!--   <topicRef xlink:href="$view" />                                            -->
	<!--  </scope>                                                                    -->
	<!--  <resourceRef xlink:href="$URI-OF-ITEM" />                                   -->
	<!-- </occurrence>                                                                -->
	<xsl:template match="tml:docbase-mapping">
		<occurrence>
			<xsl:if test="../tml:view">
			<scope>
				<topicRef>
					<xsl:attribute name="xlink:href">
						<xsl:value-of select="key('decItemKey',../tml:view)"/>
					</xsl:attribute>
				</topicRef>
			</scope>
			</xsl:if>
			<resourceRef>
  				<xsl:attribute name="xlink:href">
  					<xsl:value-of select="normalize-space(.)"/>
  				</xsl:attribute>
  			</resourceRef>
		</occurrence>
	</xsl:template>

</xsl:transform>

