<?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"
	  xmlns:t="urn:SaveResult-schema"
	  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="t:BATCH" /> 
		</xsl:element>
	</xsl:template>

	<xsl:template match="t:BATCH">
			<xsl:apply-templates select="t:FACTORY" />
			<xsl:apply-templates select="/t:BATCH/t:PANEL" />
	</xsl:template>

	<xsl:template match="t:FACTORY">
		<xsl:element name="FACTORY">
			<xsl:attribute name="NAME">
				<xsl:value-of select="t:NAME" />
			</xsl:attribute>			
			<xsl:attribute name="TESTER">
				<xsl:value-of select="t:TESTER" />
			</xsl:attribute>			
			<xsl:attribute name="FIXTURE">
				<xsl:value-of select="t:FIXTURE" />
			</xsl:attribute>			
			<xsl:attribute name="USER">
				<xsl:value-of select="t:USER" />
			</xsl:attribute>			
		</xsl:element>
	</xsl:template>

	<xsl:template match="t:PANEL">
		<xsl:element name="PANEL">
			<xsl:attribute name="ID">
				<xsl:value-of select="t:ID" />
			</xsl:attribute>
			<xsl:attribute name="COMMENT">
				<xsl:value-of select="t:COMMENT" />
			</xsl:attribute>
			<xsl:attribute name="RUNMODE">
				<xsl:value-of select="t:RUNMODE" />
			</xsl:attribute>
			<xsl:attribute name="TIMESTAMP">
				<xsl:value-of select="t:TIMESTAMP" />
			</xsl:attribute>
			<xsl:attribute name="TESTTIME">
				<xsl:value-of select="t:TESTTIME" />
			</xsl:attribute>
			<xsl:attribute name="WAITTIME">
				<xsl:value-of select="t:WAITTIME" />
			</xsl:attribute>
			<xsl:attribute name="STATUS">
				<xsl:value-of select="t:STATUS" />
			</xsl:attribute>
		</xsl:element>
		<xsl:apply-templates select="t:DUT" />

		<xsl:for-each select="t:EXTRA">
			<xsl:call-template name="parseEXTRA">
				<xsl:with-param name="WhatFor">PANEL</xsl:with-param>
				<xsl:with-param name="ParentID"></xsl:with-param>
			</xsl:call-template>
		</xsl:for-each>

	</xsl:template>

	<xsl:template match="t:DUT">
		<xsl:variable name="UID" select="flex:getDUTID()" />
		<xsl:element name="DUT">
			<xsl:attribute name="ID">
				<xsl:value-of select="t:ID" />
			</xsl:attribute>
			<xsl:attribute name="UID">
				<xsl:value-of select="$UID" />
			</xsl:attribute>
			<xsl:attribute name="STATUS">
				<xsl:value-of select="t:STATUS" />
			</xsl:attribute>
			<xsl:attribute name="TIMESTAMP">
				<xsl:value-of select="t:TIMESTAMP" />
			</xsl:attribute>
			<xsl:attribute name="TESTTIME">
				<xsl:value-of select="t:TESTTIME" />
			</xsl:attribute>
			<xsl:attribute name="PANELIDX">
				<xsl:value-of select="t:PANEL_POS" /> <!--PANEL_POS -->
			</xsl:attribute>
			<xsl:attribute name="SOCKET">
				<xsl:value-of select="t:SOCKET" />
			</xsl:attribute>
		</xsl:element>

		<xsl:for-each select="t:GROUP">
			<xsl:if test='t:NAME="Simlocks"'>
			<xsl:element name="GROUP">
				<xsl:call-template name="parseGroup">
					<xsl:with-param name="dutID"><xsl:value-of select="$UID"/></xsl:with-param>
					<xsl:with-param name="parentID" />
				</xsl:call-template>
			</xsl:element>

			<xsl:for-each select="t:TEST">
				<xsl:call-template name="parseTest">
					<xsl:with-param name="dutID"><xsl:value-of select="$UID"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>	
			</xsl:if>
		</xsl:for-each>			
	</xsl:template>

	<xsl:template name="parseGroup">
	<xsl:param name="dutID" />
	<xsl:param name="parentID" />
		<xsl:if test='name()="GROUP"'>
			<xsl:variable name="UID" select="flex:getGroupID()" />
						
				<xsl:attribute name ="NAME">
					<xsl:value-of select="t: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:if test="boolean(string(t:DESCRIPTION))">
					<xsl:attribute name ="DESCR">
						<xsl:value-of select="t:DESCRIPTION" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="boolean(string(t:DESCRIPTION))">
					<xsl:attribute name="DESCRIPTION">
						<xsl:value-of select="t:DESCRIPTION" />
					</xsl:attribute>
				</xsl:if>
				<xsl:attribute name="STATUS">
					<xsl:value-of select="t:STATUS" />
				</xsl:attribute>

				<xsl:if test="boolean(string(t:COMMENT))">
					<xsl:attribute name="COMMENT">
						<xsl:value-of select="t:COMMENT" />
					</xsl:attribute>
				</xsl:if>			
		</xsl:if>
	</xsl:template>

	<xsl:template name="parseTest">
	<xsl:param name="dutID" />
	<xsl:param name="groupID" />
		<xsl:choose>
		<xsl:when test='name()="TEST"'>
			<xsl:variable name="UID" select="flex:getTestID()" />
			<xsl:element name="TEST">
				<xsl:attribute name="NAME">
					<xsl:value-of select="t:NAME" />
				</xsl:attribute>
				<xsl:if test="boolean(string(t:DESCRIPTION))">
					<xsl:attribute name="DESCRIPTION">
						<xsl:value-of select="t:DESCRIPTION" />
					</xsl:attribute>
				</xsl:if>
				<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="t:STATUS" />
				</xsl:attribute>
				<xsl:if test="boolean(string(t:HILIM))">
					<xsl:attribute name="HILIM">
						<xsl:value-of select="t:HILIM" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="boolean(string(t:LOLIM))">
					<xsl:attribute name="LOLIM">
						<xsl:value-of select="t:LOLIM" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="boolean(string(t:UNIT))">
					<xsl:attribute name="UNIT">
						<xsl:value-of select="t:UNIT" />
					</xsl:attribute>
				</xsl:if>

				<xsl:attribute name="VALUE">
					<xsl:value-of select="t:VALUE" />
				</xsl:attribute>

				<xsl:if test="boolean(string(t:RULE))">
					<xsl:attribute name="RULE">
						<xsl:value-of select="t:RULE" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="boolean(string(t:TARGET))">
					<xsl:attribute name="TARGET">
						<xsl:value-of select="t:TARGET" />
					</xsl:attribute>
				</xsl:if>
				<xsl:if test="boolean(string(t:DATATYPE))">
					<xsl:attribute name="DATATYPE">
						<xsl:value-of select="t:DATATYPE" />
					</xsl:attribute>
				</xsl:if>
			</xsl:element>

			<xsl:for-each select="t:EXTRA">
				<xsl:call-template name="parseEXTRA">
					<xsl:with-param name="WhatFor">TEST</xsl:with-param>
					<xsl:with-param name="ParentID"><xsl:value-of select="$UID"/></xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>

		</xsl:when>
	</xsl:choose>
	</xsl:template>

	<xsl:template name="parseEXTRA">
	<xsl:param name="WhatFor" />
	<xsl:param name="ParentID" />
		<xsl:choose>
		<xsl:when test='name()="EXTRA"'>
			<xsl:element name="EXTRA">
				<xsl:attribute name="FOR">
					<xsl:value-of select="$WhatFor" />
				</xsl:attribute>
				<xsl:attribute name="PID">
					<xsl:value-of select="$ParentID" />
				</xsl:attribute>
				<xsl:for-each select="child::*">
					<xsl:attribute name="TAG" >
						<xsl:value-of select="name()" />
					</xsl:attribute>
					<xsl:attribute name="VAL" >
						<xsl:value-of select="text()" />
					</xsl:attribute>
				</xsl:for-each>
			</xsl:element>
		</xsl:when>
	</xsl:choose>
	</xsl:template>

</xsl:transform>

<!-- last update 04/24/2003 -->
<!-- 04/07/2006 Tony filtered blank attribute for GROUP and TEST. Result test is too big -->

