<?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 maxDUTID = 1;
		var symDUTTab;
		function getDUTID(strItem, strProcess, strSession) 
		{
			if (typeof(symDUTTab)=='undefined') symDUTTab = new Object();
			if (!symDUTTab[strItem[0].text, strProcess[0].text, strSession[0].text]) symDUTTab[strItem[0].text, 

strProcess[0].text, strSession[0].text] = maxDUTID++; 
			return symDUTTab[strItem[0].text, strProcess[0].text, strSession[0].text]
		}

		var groupID=1;
		var testID=1;
		function getGROUPID() {return groupID++; }
		function getTESTID() {return testID++; }

	]]>
	</ms:script>

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

	<xsl:template match="ProcessStepStatus">
		<xsl:if test="@status='PASSED' or @status='FAILED' or @status='ERROR'">
			<xsl:variable name="UID" select="flex:getGROUPID()" />
			<xsl:variable name="DUT" select="flex:getDUTID(@itemInstanceId, @itemProcessRef, @sessionRef)" />
			<xsl:element name="GROUP">
				<xsl:attribute name="UID">
					<xsl:value-of select="$UID" />
				</xsl:attribute>

				<xsl:attribute name="DUT">
					<xsl:value-of select="$DUT" />
				</xsl:attribute>

				<xsl:attribute name="NAME">
					<xsl:value-of select="@processStepId" />
				</xsl:attribute>	
				
				<xsl:attribute name="PARENT">
				</xsl:attribute>	

				<xsl:attribute name="STATUS">
					<xsl:choose>
					<xsl:when test="@status='PASSED'">
						<xsl:text>PASSED</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>FAILED</xsl:text>
					</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>								

				<xsl:attribute name="TIMESTAMP">
					<xsl:value-of select="@dateTime" />
				</xsl:attribute>

				<xsl:choose>
					<xsl:when test="(@processStepId='Program Manufacturing Sector' or @processStepId='PGM Secure Sector') and boolean(translate(@comment,' ', 

''))">
						<xsl:attribute name="COMMENT">
							<xsl:text>Values saved into measurement</xsl:text>
						</xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="COMMENT">
							<xsl:value-of select="@comment" />
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:element>
			<xsl:for-each select="Measurement">
				<xsl:call-template name="Measurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>

			<!--Tony 02/15/2005 commented this out -->
			<xsl:if test="@processStepId='PGM Secure Sector' and boolean(translate(@comment,' ', ''))">
				<xsl:variable name="HashOfSystemCertificate2" select="translate(substring-after(@comment, 'HASH2='), ' ', '')" 

/>
				<xsl:variable name="strTemp1" select="substring-before(@comment, 'HASH2=')" />
				<xsl:variable name="HashOfSystemCertificate1" select="translate(substring-after($strTemp1, 'HASH1='), ' ', '')" 

/>
				<xsl:variable name="strTemp2" select="substring-before(@comment, 'HASH1=')" />
				<xsl:variable name="RandomSeed" select="translate(substring-after($strTemp2, 'RANSEED='), ' ', 

'')" />
				<xsl:variable name="strTemp3" select="substring-before(@comment, 'RANSEED=')" />
				<xsl:variable name="SYSID" select="translate(substring-after($strTemp3, 'SYSID='), ' ', 

'')" />
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
					<xsl:with-param name="name"><xsl:text>RandomSeed</xsl:text></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="$RandomSeed"/></xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
			<xsl:with-param name="name"><xsl:text>HashOfSystemCertificate2</xsl:text></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="$HashOfSystemCertificate2"/></xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
			<xsl:with-param name="name"><xsl:text>HashOfSystemCertificate1</xsl:text></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="$HashOfSystemCertificate1"/></xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
					<xsl:with-param name="name"><xsl:text>SYSID</xsl:text></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="$SYSID"/></xsl:with-param>
				</xsl:call-template>
			</xsl:if>

			<!-- Tony 05/23/2006 added this part for new BCM50 model -->
			<xsl:if test="@processStepId='Program Manufacturing Sector' and boolean(translate(@comment,' ', ''))">
				<xsl:variable name="Key2" select="translate(substring-after(@comment, 'Key2 = '), ' ', '')" 

/>
				<xsl:variable name="strTemp1" select="substring-before(@comment, 'Key2 = ')" />
				<xsl:variable name="Key1" select="translate(substring-after($strTemp1, 'Key1 = '), ' ', '')" 

/>
				<xsl:variable name="strTemp2" select="substring-before(@comment, 'Key1 = ')" />
				<xsl:variable name="Hash6" select="translate(substring-after($strTemp2, 'Hash6 = '), ' ', 

'')" />
				<xsl:variable name="strTemp3" select="substring-before(@comment, 'Hash6 = ')" />
				<xsl:variable name="Hash5" select="translate(substring-after($strTemp3, 'Hash5 = '), ' ', 

'')" />
				<xsl:variable name="strTemp4" select="substring-before(@comment, 'Hash5 = ')" />
				<xsl:variable name="Hash4" select="translate(substring-after($strTemp4, 'Hash4 = '), ' ', 

'')" />
				<xsl:variable name="strTemp5" select="substring-before(@comment, 'Hash4 = ')" />
				<xsl:variable name="Hash3" select="translate(substring-after($strTemp5, 'Hash3 = '), ' ', 

'')" />
				<xsl:variable name="strTemp6" select="substring-before(@comment, 'Hash3 = ')" />
				<xsl:variable name="Hash2" select="translate(substring-after($strTemp6, 'Hash2 = '), ' ', 

'')" />
				<xsl:variable name="strTemp7" select="substring-before(@comment, 'Hash2 = ')" />
				<xsl:variable name="Hash1" select="translate(substring-after($strTemp7, 'Hash1 = '), ' ', 

'')" />
				<xsl:variable name="strTemp8" select="substring-before(@comment, 'Hash1 = ')" />
				<xsl:variable name="SYSID" select="translate(substring-after($strTemp8, 'SYSID='), ' ', 

'')" />
				<xsl:if test="boolean($SYSID)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>SYSID</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$SYSID"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash1)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>HashOfSystemCertificate1</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash1"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash2)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>HashOfSystemCertificate2</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash2"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash3)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Hash3</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash3"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash4)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Hash4</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash4"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash5)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Hash5</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash5"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Hash6)">
					<xsl:call-template name="CreateMeasurement">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Hash6</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Hash6"/></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Key1)">
					<xsl:call-template name="splitString">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Key1</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Key1"/></xsl:with-param>
						<xsl:with-param name="idx"><xsl:text>10000</xsl:text></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:if test="boolean($Key2)">
					<xsl:call-template name="splitString">
						<xsl:with-param name="dutID"><xsl:value-of select="$DUT"/></xsl:with-param>
						<xsl:with-param name="groupID"><xsl:value-of select="$UID"/></xsl:with-param>
						<xsl:with-param name="name"><xsl:text>Key2</xsl:text></xsl:with-param>
						<xsl:with-param name="value"><xsl:value-of select="$Key2"/></xsl:with-param>
						<xsl:with-param name="idx"><xsl:text>10000</xsl:text></xsl:with-param>
					</xsl:call-template>
				</xsl:if>
			</xsl:if>
		</xsl:if>
	</xsl:template>

	<xsl:template name="splitString">
	<xsl:param name="dutID" />
	<xsl:param name="groupID" />
	<xsl:param name="name" />
	<xsl:param name="value" />
	<xsl:param name="idx" />
		<xsl:choose>
			<xsl:when test="string-length($value) &lt;= 200">
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$dutID"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$groupID"/></xsl:with-param>
					<xsl:with-param name="name"><xsl:value-of select="concat($name, '_', $idx)"/></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="$value"/></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="CreateMeasurement">
					<xsl:with-param name="dutID"><xsl:value-of select="$dutID"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$groupID"/></xsl:with-param>
					<xsl:with-param name="name"><xsl:value-of select="concat($name, '_', $idx)"/></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="substring($value, 1, 200)"/></xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="splitString">
					<xsl:with-param name="dutID"><xsl:value-of select="$dutID"/></xsl:with-param>
					<xsl:with-param name="groupID"><xsl:value-of select="$groupID"/></xsl:with-param>
					<xsl:with-param name="name"><xsl:value-of select="$name"/></xsl:with-param>
					<xsl:with-param name="value"><xsl:value-of select="substring($value, 201)"/></xsl:with-param>
					<xsl:with-param name="idx"><xsl:value-of select="$idx + 1"/></xsl:with-param>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="Measurement">
	<xsl:param name="dutID" />
	<xsl:param name="groupID" />
		<xsl:if test="@status='PASSED' or @status='FAILED' or @status='ERROR'">
			<xsl:variable name="UID" select="flex:getTESTID()" />
			<xsl:element name="TEST">
					<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="NAME">
						<xsl:value-of select="@measurementId" />
					</xsl:attribute>	

					<xsl:attribute name="DESCRIPTION">
						<xsl:value-of select="@type" />
					</xsl:attribute>	

					<xsl:attribute name="STATUS">
						<xsl:choose>
						<xsl:when test="@status='PASSED'">
							<xsl:text>PASSED</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>FAILED</xsl:text>
						</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>

					<!-- 08/02/2006 added this code -->
					<xsl:choose>
					<xsl:when test="@status='FAILED'">
						<xsl:attribute name="FAILURECODE">
							<xsl:value-of select="@type" />
						</xsl:attribute>
					</xsl:when>
					</xsl:choose>
					
					<xsl:apply-templates/>	
			</xsl:element>
		</xsl:if>
	</xsl:template>

	<xsl:template match="MeasuredNumeric">
		<xsl:attribute name="VALUE">
			<xsl:value-of select="@value" />
		</xsl:attribute>
		<xsl:attribute name="UNIT">
			<xsl:value-of select="@units" />
		</xsl:attribute>
	</xsl:template>

	<xsl:template match="ExpectedNumeric">
		<xsl:attribute name="RULE">
			<xsl:value-of select="@comparator" />
		</xsl:attribute>

		<xsl:attribute name="HILIM">
			<xsl:value-of select="@maximum" />
		</xsl:attribute>

		<xsl:attribute name="LOLIM">
			<xsl:value-of select="@minimum" />
		</xsl:attribute>
	</xsl:template>

	<xsl:template match="ItemProcessStatus">
		<xsl:if test="@status='PASSED' or @status='FAILED' or @status='ERROR'">
			<xsl:variable name="UID" select="flex:getDUTID(@itemInstanceId, @itemProcessId, @sessionRef)" />
			<xsl:element name="DUT">
				<xsl:attribute name="ID">
					<xsl:value-of select="@itemInstanceId" />
				</xsl:attribute>
				<xsl:attribute name="UID">
					<xsl:value-of select="$UID" />
				</xsl:attribute>
				<xsl:attribute name="STATUS">
					<xsl:choose>
					<xsl:when test="@status='PASSED'">
						<xsl:text>PASSED</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>FAILED</xsl:text>
					</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:attribute name="TIMESTAMP">
					<xsl:value-of select="@dateTime" />
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:template>

	<xsl:template name="CreateMeasurement">
	<xsl:param name="dutID" />
	<xsl:param name="groupID" />
	<xsl:param name="name" />
	<xsl:param name="value" />
		<xsl:variable name="UID" select="flex:getTESTID()" />
		<xsl:element name="TEST">
				<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="NAME">
					<xsl:value-of select="$name" />
				</xsl:attribute>	

				<xsl:attribute name="DESCRIPTION">
					<xsl:value-of select="$name" />
				</xsl:attribute>	

				<!-- 05/05/2006 added FailureCode. It is same as Descr
				<xsl:choose>
				<xsl:when test="@status='FAILED'">
					<xsl:attribute name="FAILURECODE">
						<xsl:value-of select="$name" />
					</xsl:attribute>
				</xsl:when>
				</xsl:choose>  -->

				<xsl:attribute name="STATUS">
					<xsl:text>PASS</xsl:text>
				</xsl:attribute>

				<xsl:attribute name="VALUE">
					<xsl:value-of select="$value" />
				</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- [@type='FlexflowStationId'] -->
	<xsl:template match="ProcessSessionStart">
		<xsl:element name="FACTORY">
			<xsl:choose>
				<xsl:when test="boolean(FixtureTooling[@type='FlexflowStationId']/@serialNumber)">
					<xsl:attribute name="TESTER">
						<xsl:value-of select="FixtureTooling[@type='FlexflowStationId']/@serialNumber 

" />
					</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="TESTER">
						<xsl:value-of select="Entity/@stationId" />
					</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:attribute name="USER">
				<xsl:value-of select="Operator/@employeeId" />
			</xsl:attribute>
		</xsl:element>
	</xsl:template>
</xsl:transform>

<!--modified (handle 'ERROR' status) 02/08/2004 -->
<!--modified (disable parsing "comment" attribute. SP will do it) 02/16/2004 -->
<!--modified 01/19/2006 Parsing FixtureTooling[@type='FlexflowStationId']/@serialNumber -->
<!--modified 05/05/2006 added FailureCode. It is same as DESCRIPTION -->
<!--modified 07/31/2006 changed to Program Manufacturing Sector (used to be PGM Mfg Sector) -->
<!--modified 08/01/2006 added SYSID for Program Manufacturing Sector -->
<!--08/02/2006 added code to return FAILURECODE -->

