<?xml version="1.0" encoding="utf-8"?>
<xsl:transform 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	  xmlns:ms="urn:schemas-microsoft-com:xslt"
	  xmlns:flex="urn:schemas-flextronics-com:xslt"
	  version="1.0">
	 
	<xsl:output method="xml" version="1.0" encoding="utf-8"/>

	<ms:script language="JScript" implements-prefix="flex">
	<![CDATA[
		var groupID=1;
		var dutID=1;
		var testID=1;
		function getGroupID() {return groupID++; }
		function getDUTID() {return dutID++; }
		function getTESTID() {return testID++; }
	]]>
	</ms:script>

	<xsl:template match="/">
		<xsl:element name="FLEXFLOW">
			<xsl:apply-templates select="UNIT" />
		</xsl:element>
	</xsl:template>

	<xsl:template match="UNIT">
		<xsl:variable name="UID" select="flex:getDUTID()" />

		<xsl:element name="FACTORY">
			<xsl:attribute name="TESTER">
				<xsl:value-of select="TEST[position()=1]/STATION" />
			</xsl:attribute>
			<xsl:attribute name="FIXTURE">
				<xsl:value-of select="TEST[position()=1]/FIXTURE" />
			</xsl:attribute>
		</xsl:element>

		<xsl:element name="PANEL">
			<xsl:attribute name="ID">
				<xsl:value-of select="FID" />
			</xsl:attribute>
		</xsl:element>

		<xsl:element name="DUT">
			<xsl:attribute name="ID">
				<xsl:value-of select="FID" />
			</xsl:attribute>
			<xsl:attribute name="UID">
				<xsl:value-of select="$UID" />
			</xsl:attribute>
			<xsl:attribute name="TIMESTAMP">
				<xsl:value-of select="TIMESTAMP" />
			</xsl:attribute>
			<xsl:attribute name="STATUS">
				<xsl:value-of select="TEST[1]/RESULT" />
			</xsl:attribute>
		</xsl:element>

		<!-- the DTD allows multiple TEST. We will handle the first one (use its STATION). 
		     The rest are ignored -->
		<xsl:for-each select="TEST[position()=1]">
			<xsl:call-template name="parseTEST">
				<xsl:with-param name="dutID"><xsl:value-of select="$UID"/></xsl:with-param>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="parseTEST">
		<xsl:param name="dutID" />
		<xsl:choose>
			<xsl:when test="boolean(TEST_DATA)">
				<xsl:for-each select="TEST_DATA">
					<xsl:variable name="UID" select="flex:getGroupID()" />
					<xsl:element name="GROUP">
						<xsl:attribute name="UID">
							<xsl:value-of select="$UID" />
						</xsl:attribute>
						<xsl:attribute name="DUT">
							<xsl:value-of select="$dutID" />
						</xsl:attribute>
						<xsl:attribute name ="DESCR">
							<xsl:value-of select="parent::TEST/CONFIG_FILE" />
						</xsl:attribute>
						<xsl:attribute name ="PARENT" /> <!-- NULL PARENT -->
						<xsl:attribute name="NAME">
							<xsl:value-of select="parent::TEST/FAIL_CODE" />
						</xsl:attribute>
						<xsl:attribute name="STEPGROUP">
							<xsl:value-of select="parent::TEST/SW_VER" />
						</xsl:attribute>
						<xsl:attribute name="LOOPIDX">
							<xsl:value-of select="LOOP_COUNT" />
						</xsl:attribute>
						<xsl:attribute name="STATUS">
							<xsl:value-of select="parent::TEST/RESULT" />
						</xsl:attribute>
						<xsl:attribute name="TOTALTIME">
							<xsl:value-of select="parent::TEST/DURATION" />
						</xsl:attribute>
						<xsl:attribute name="TIMESTAMP">
							<xsl:value-of select="ancestor::UNIT/TIMESTAMP" />
						</xsl:attribute>
						<xsl:attribute name="TYPE">
							<xsl:value-of select="parent::TEST/STATUS" />
						</xsl:attribute>
						<xsl:attribute name="COMMENT">
							<xsl:value-of select="parent::TEST/ACTION" />
						</xsl:attribute>
					</xsl:element>
					<xsl:call-template name="parseTESTDATA">
						<xsl:with-param name="dutID"><xsl:value-of select="$dutID"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="UID" select="flex:getGroupID()" />
				<xsl:element name="GROUP">
					<xsl:attribute name="UID">
						<xsl:value-of select="$UID" />
					</xsl:attribute>
					<xsl:attribute name="DUT">
						<xsl:value-of select="$dutID" />
					</xsl:attribute>
					<xsl:attribute name ="DESCR">
						<xsl:value-of select="CONFIG_FILE" />
					</xsl:attribute>
					<xsl:attribute name ="PARENT" />
					<xsl:attribute name="NAME">
						<xsl:value-of select="FAIL_CODE" />
					</xsl:attribute>
					<xsl:attribute name="STEPGROUP">
						<xsl:value-of select="SW_VER" />
					</xsl:attribute>
					<xsl:attribute name="STATUS">
						<xsl:value-of select="RESULT" />
					</xsl:attribute>
					<xsl:attribute name="TOTALTIME">
						<xsl:value-of select="DURATION" />
					</xsl:attribute>
					<xsl:attribute name="TIMESTAMP">
						<xsl:value-of select="ancestor::UNIT/TIMESTAMP" />
					</xsl:attribute>
					<xsl:attribute name="TYPE">
						<xsl:value-of select="STATUS" />
					</xsl:attribute>
					<xsl:attribute name="COMMENT">
						<xsl:value-of select="ACTION" />
					</xsl:attribute>
				</xsl:element>
				<xsl:call-template name="parseTESTDATA">
					<xsl:with-param name="dutID"><xsl:value-of select="$dutID"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="parseTESTDATA">
		<xsl:param name="dutID" />
		<xsl:param name="groupID" />
		<xsl:variable name="UID" select="flex:getTESTID()" />
		<xsl:element name="TEST">
			<xsl:attribute name="NAME">
				<xsl:value-of select="PARAM_NAME" />
			</xsl:attribute>
			<xsl:attribute name="UID">
				<xsl:value-of select="$UID" />
			</xsl:attribute>
			<xsl:attribute name="DUT">
				<xsl:value-of select="$dutID" />
			</xsl:attribute>
			<xsl:attribute name="GROUP">
				<xsl:value-of select="$groupID" />
			</xsl:attribute>
			<xsl:attribute name="STATUS">
				<xsl:value-of select="RESULT" />
			</xsl:attribute>
			<xsl:attribute name="FAILURECODE">
				<xsl:value-of select="FAIL_CODE" />
			</xsl:attribute>
			<xsl:if test="boolean(string(UPPER_LIMIT))">
				<xsl:attribute name="HILIM">
					<xsl:value-of select="UPPER_LIMIT" />
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="boolean(string(LOWER_LIMIT))">
				<xsl:attribute name="LOLIM">
					<xsl:value-of select="LOWER_LIMIT" />
				</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="UNIT">
				<xsl:value-of select="UOM" />
			</xsl:attribute>
			<xsl:attribute name="VALUE">
				<xsl:value-of select="VALUE" />
			</xsl:attribute>
			<xsl:attribute name="TARGET">
				<xsl:value-of select="PRECISION" />
			</xsl:attribute>
			<xsl:attribute name="DATATYPE">
				<xsl:value-of select="DATA_TYPE" />
			</xsl:attribute>
		</xsl:element>
		<xsl:if test="boolean(string(STATUS))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">STATUS</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="STATUS"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
		<xsl:if test="boolean(string(NOTE))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">NOTE</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="NOTE"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
		<xsl:if test="boolean(string(FAIL_MESSAGE))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">FAIL_MESSAGE</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="FAIL_MESSAGE"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
		<xsl:if test="boolean(string(FAIL_CODE))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">FAIL_CODE</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="FAIL_CODE"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
		<xsl:if test="boolean(string(ACTION))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">ACTION</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="ACTION"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
		<xsl:if test="boolean(string(ERROR_LIST))">
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">TEST</xsl:attribute>
				<xsl:attribute name="PID"><xsl:value-of select="$UID"/></xsl:attribute>
				<xsl:attribute name="TAG">ERROR_LIST</xsl:attribute>
				<xsl:attribute name="VAL"><xsl:value-of select="ERROR_LIST"/></xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:template>
</xsl:transform>