<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0"
	xmlns:xalan="http://xml.apache.org/xalan"
	exclude-result-prefixes="xalan">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:template match="report">
	<xsl:copy>
		<xsl:if test="node()">
			<xsl:attribute name="reportValue">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:if>
	    <xsl:apply-templates select="@*"/>		
	</xsl:copy>
</xsl:template>
	
<xsl:template match="ct">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='ts')]|node()"/>	
		<xsl:for-each select="xalan:tokenize(@ts,' ')">
			<xsl:element name="tm">
				<xsl:attribute name="idref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
				<xsl:attribute name="serverIdref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
			</xsl:element>				
		</xsl:for-each>
	</xsl:copy>	
</xsl:template>

<xsl:template match="tv">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='ts')]|node()"/>	
		<xsl:for-each select="xalan:tokenize(@ts,' ')">
			<xsl:element name="tm">
				<xsl:attribute name="idref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
				<xsl:attribute name="serverIdref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
			</xsl:element>				
		</xsl:for-each>
	</xsl:copy>	
</xsl:template>
	
<xsl:template match="defGroup">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='types')]|node()"/>	
		<xsl:for-each select="xalan:tokenize(@types,' ')">
			<xsl:element name="type">
				<xsl:attribute name="idref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
				<xsl:attribute name="serverIdref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
			</xsl:element>				
		</xsl:for-each>
	</xsl:copy>	
</xsl:template>	

<!--xsl:template match="doc/text">
	<xsl:copy>
		<xsl:attribute name="text">
			<xsl:value-of select="."/>		
		</xsl:attribute>
	</xsl:copy>	
</xsl:template-->

<xsl:template match="doc">
	<xsl:copy>
		<xsl:if test="text">
			<xsl:attribute name="text">
				<xsl:value-of select="."/>		
			</xsl:attribute>			
		</xsl:if>		
		<xsl:apply-templates select="@*|node()[not(name()='text')]"/>			
	</xsl:copy>	
</xsl:template>
	
		
<xsl:template match="cv">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='cs')]|node()"/>	
		<xsl:for-each select="xalan:tokenize(@cs,' ')">
			<xsl:element name="ct">
				<xsl:attribute name="idref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
			</xsl:element>				
		</xsl:for-each>
	</xsl:copy>	
</xsl:template>

<xsl:template match="catv">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='cats')]|node()"/>	
		<xsl:for-each select="xalan:tokenize(@cats,' ')">
			<xsl:element name="category">
				<xsl:attribute name="idref">
					<xsl:value-of select="."/>
				</xsl:attribute>			
			</xsl:element>				
		</xsl:for-each>
	</xsl:copy>	
</xsl:template>

<xsl:template match="lexGroup[@type]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='type')]|node()"/>	
		<xsl:element name="type">
			<xsl:attribute name="idref">
				<xsl:value-of select="@type"/>
			</xsl:attribute>			
			<xsl:attribute name="serverIdref">
				<xsl:value-of select="@type"/>
			</xsl:attribute>			
		</xsl:element>				
	</xsl:copy>
</xsl:template>

<!--xsl:template match="tm[@l]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='l')]|node()"/>	
		<xsl:element name="l">
			<xsl:attribute name="idref">
				<xsl:value-of select="@l"/>
			</xsl:attribute>			
			<xsl:attribute name="serverIdref">
				<xsl:value-of select="@l"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template-->		

<!--NEW - handles idmanagement for scoring-->
<xsl:template match="tm">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>	
		<xsl:attribute name="serverId">
			<xsl:value-of select="@id"/>
		</xsl:attribute>			

		<xsl:if test="@l">
		
		<xsl:element name="l">
			<xsl:attribute name="idref">
				<xsl:value-of select="@l"/>
			</xsl:attribute>			
			<xsl:attribute name="serverIdref">
				<xsl:value-of select="@l"/>
			</xsl:attribute>			
		</xsl:element>		
		
		</xsl:if>
	</xsl:copy>
</xsl:template>		

<xsl:template match="type[@id]">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>	
		<xsl:attribute name="serverId">
			<xsl:value-of select="@id"/>
		</xsl:attribute>			
	</xsl:copy>
</xsl:template>		
<!--END NEW-->
				
<xsl:template match="r[@tv]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='tv')]|node()"/>	
		<xsl:element name="tv">
			<xsl:attribute name="idref">
				<xsl:value-of select="@tv"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template>		
		
<xsl:template match="t[@t]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='t')]|node()"/>	
		<xsl:element name="tm">
			<xsl:attribute name="idref">
				<xsl:value-of select="@t"/>
			</xsl:attribute>			
			<xsl:attribute name="serverIdref">
				<xsl:value-of select="@t"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template>		

<xsl:template match="y[@y]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='y')]|node()"/>	
		<xsl:element name="type">
			<xsl:attribute name="idref">
				<xsl:value-of select="@y"/>
			</xsl:attribute>			
			<xsl:attribute name="serverIdref">
				<xsl:value-of select="@y"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template>		
	
<xsl:template match="c[@c]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='c')]|node()"/>	
		<xsl:element name="ct">
			<xsl:attribute name="idref">
				<xsl:value-of select="@c"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template>		
		
<xsl:template match="td[@t]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='t')]|node()"/>	
		<xsl:element name="t">		
			<xsl:attribute name="idref">
				<xsl:value-of select="@t"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:copy>
</xsl:template>		

<xsl:template match="ld[@l]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='l')]|node()"/>	
		<xsl:element name="l">
			<xsl:attribute name="idref">
				<xsl:value-of select="@l"/>
			</xsl:attribute>
		</xsl:element>			
	</xsl:copy>
</xsl:template>		
	
<xsl:template match="yd[@y]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='y')]|node()"/>	
		<xsl:element name="y">
			<xsl:attribute name="idref">
				<xsl:value-of select="@y"/>
			</xsl:attribute>			
		</xsl:element>
	</xsl:copy>
</xsl:template>			
	
<xsl:template match="cd[@c]">
	<xsl:copy>
		<xsl:apply-templates select="@*[not(name()='c')]|node()"/>	
		<xsl:element name="c">
			<xsl:attribute name="idref">
				<xsl:value-of select="@c"/>
			</xsl:attribute>
		</xsl:element>		
	</xsl:copy>
</xsl:template>			
			
<xsl:template match="@*|*">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>