<?xml version="1.0" encoding="UTF-8"?>

<!-- 
FDT: device identification transformation for MT device identification xml files
-->
<xsl:transform 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		xmlns:str="http://xsltsl.org/string" 
		xmlns:ident="x-schema:DTMIdentSchema.xml"
		xmlns:fdt="x-schema:FDTDataTypesSchema.xml"
		xmlns:scanident="x-schema:DTMScanIdentSchema.xml"
		xmlns:devident="x-schema:DTMDeviceTypeIdentSchema.xml"
		xmlns:mtident="x-schema:FDTmtIdent2Schema.xml"
		xmlns:mtdevice="x-schema:FDTmtDevice2TypeIdentSchema.xml"
		xmlns:mtscan="x-schema:FDTmtScan2IdentSchema.xml" version="2.0">
	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
		<!-- 
FDT: version of this file
	-->

	<xsl:variable name="FileVersion">
		<xsl:number value="1.21"/>
	</xsl:variable>
	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- 
root: transform device or scan identification
-->
	<xsl:template match="/">
			<xsl:apply-templates select="//mtscan:ScanIdentifications"/>
			<xsl:apply-templates select="//mtdevice:DeviceIdentifications"/>
	</xsl:template>
<!-- 
HART identification list
-->
	<xsl:template match="mtdevice:DeviceIdentifications">
		<xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTxxxIdentTransformation.xsl after transformation of xxxDTMDeviceIdentificationInstance.xml --&gt;
	&lt;FDT xmlns="x-schema:DTMDeviceTypeIdentSchema.xml" 
	 xmlns:ident="x-schema:DTMIdentSchema.xml" xmlns:fdt="x-schema:FDTDataTypesSchema.xml" &gt;</xsl:text>
				<xsl:element name="DeviceIdentifications">
					<xsl:apply-templates select="mtdevice:DeviceIdentification"/>
				</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>
<!-- 
HART catalog identifcation
-->
	<xsl:template match="mtdevice:DeviceIdentification">
			<xsl:element name="DeviceIdentification">
				<xsl:attribute name="ident:idDTMSupportLevel">
					<xsl:value-of select="@mtident:idDTMSupportLevel"/>
				</xsl:attribute>
				<xsl:apply-templates select="mtdevice:IdBusProtocol"/>
<!--				<xsl:apply-templates select="mtdevice:IdBusProtocolVersion"/>-->
				<xsl:apply-templates select="mtdevice:IdManufacturer"/>
				<xsl:apply-templates select="mtdevice:IdTypeID"/>
				<xsl:apply-templates select="mtdevice:IdSoftwareRevision"/>
				<xsl:apply-templates select="mtdevice:IdHardwareRevision"/>
				<xsl:element name="IdValues">
					<xsl:apply-templates select="mtdevice:DeviceCommandRevisionLevel"/>
					<xsl:apply-templates select="mtdevice:ManufacturerSpecificExtension"/>
				</xsl:element>
			</xsl:element>
	</xsl:template>
<!-- 
HART scan list
-->
	<xsl:template match="mtscan:ScanIdentifications">
			<xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTxxxIdentTransformation.xsl after transformation of xxxDTMScanIdentificationInstance.xml --&gt;&lt;FDT xmlns="x-schema:DTMScanIdentSchema.xml" 
	 xmlns:ident="x-schema:DTMIdentSchema.xml" xmlns:fdt="x-schema:FDTDataTypesSchema.xml" &gt;</xsl:text>
				<xsl:element name="ScanIdentifications">
					<xsl:attribute name="fdt:busCategory">
						<xsl:value-of select="@fdt:busCategory"/>
					</xsl:attribute>
					<xsl:attribute name="resultState">
						<xsl:value-of select="@resultState"/>
					</xsl:attribute>
					<xsl:apply-templates select="mtscan:ScanIdentification"/>
				</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>


<!-- 
HART scan identifcation
-->
	<xsl:template match="mtscan:ScanIdentification">
			<xsl:element name="ScanIdentification">
				<xsl:apply-templates select="@configuredState" />
				<xsl:apply-templates select="fdt:CommunicationError"/>
				<xsl:apply-templates select="mtscan:IdBusProtocol"/>
<!-- 				<xsl:apply-templates select="mtscan:IdBusProtocolVersion"/>-->
				<xsl:apply-templates select="mtscan:IdAddress"/>			
				<xsl:apply-templates select="mtscan:IdManufacturer"/>
				<xsl:apply-templates select="mtscan:IdTypeID"/>
				<xsl:apply-templates select="mtscan:IdSoftwareRevision"/>
				<xsl:apply-templates select="mtscan:IdHardwareRevision"/>
				<xsl:apply-templates select="mtscan:IdTag"/>
<!-- 				<xsl:apply-templates select="mtscan:IdSerialNumber"/> -->
				<xsl:element name="IdValues">
					<xsl:apply-templates select="mtscan:DeviceCommandRevisionLevel"/>
					<xsl:apply-templates select="mtscan:ManufacturerSpecificExtension"/>
				</xsl:element>
			</xsl:element>
	</xsl:template>
<!-- 
configured attribute
-->
	<xsl:template match="@configuredState">
		<xsl:attribute name="configuredState">
			<xsl:value-of select="."/>
		</xsl:attribute>
	</xsl:template>

<!-- 
HartScanIdentification: device tag, just copy
-->
	<xsl:template match="mtscan:IdTag">
		<xsl:element name="IdDeviceTag">
			<xsl:attribute name="ident:value"><xsl:value-of select="@mtident:tag"/></xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Tag</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
HartScanIdentification: communicationError
-->
	<xsl:template match="fdt:CommunicationError">
		<xsl:element name="fdt:CommunicationError">
			<xsl:attribute name="communicationError"><xsl:value-of select="@communicationError"/></xsl:attribute>
			<xsl:attribute name="tag"><xsl:value-of select="@tag"/></xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
HartScanIdentification: serialnumber

	<xsl:template match="mtscan:IdSerialNumber">
		<xsl:element name="IdSerialNumber">
			<xsl:attribute name="ident:value"><xsl:value-of select="@mtident:deviceID"/></xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Device Identification Number</xsl:attribute>
		</xsl:element>
	</xsl:template>
--><!-- 
HartScanIdentification: busaddress, just copy
-->
	<xsl:template match="mtscan:IdAddress">
		<xsl:element name="IdAddress">
			<xsl:attribute name="ident:value"><xsl:value-of select="@mtident:shortAddress"/></xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Polling Address</xsl:attribute>
		</xsl:element>
	</xsl:template>

<!-- 
HartScanIdentification: CommandRevision, saved as protocol specific value

	<xsl:template match="mtdevice:DeviceCommandRevisionLevel">
		<xsl:param name="value" select="@mtident:deviceCommandRevisionLevel"/>
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">DeviceCommandRevisionLevel</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">Device Revision Level</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:DeviceCommandRevisionLevel">
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">DeviceCommandRevisionLevel</xsl:attribute>
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:deviceCommandRevisionLevel"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Device Revision Level</xsl:attribute>
		</xsl:element>
	</xsl:template>
--><!-- 
Hart Manufacaturer

	<xsl:template match="mtdevice:IdManufacturer">
		<xsl:param name="manid" select="@mtident:manufacturerIdentificationCode"/>
		<xsl:element name="IdManufacturer">
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$manid"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">Manufacturer Identification Code</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:IdManufacturer">
		<xsl:element name="IdManufacturer">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:manufacturerIdentificationCode"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Manufacturer Identification Code</xsl:attribute>
		</xsl:element>
	</xsl:template>
--><!-- 
DevId:IdBusProtocol

	<xsl:template match="mtdevice:IdBusProtocol">
		<xsl:param name="manid" select="@mtident:busProtocol"/>
		<xsl:element name="IdBusProtocol">
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$manid"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">mt</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:IdBusProtocol">
		<xsl:element name="IdBusProtocol">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:busProtocol"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">excom Communication</xsl:attribute>
		</xsl:element>
	</xsl:template>
	-->
<!-- 
DevId:IdBusProtocolVersion

	<xsl:template match="mtdevice:IdBusProtocolVersion">
		<xsl:param name="manid" select="@mtident:universalCommandRevisionLevel"/>
		<xsl:element name="IdBusProtocolVersion">
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$manid"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">MT Revision</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:IdBusProtocolVersion">
		<xsl:element name="IdBusProtocolVersion">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:universalCommandRevisionLevel"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">MT Revision</xsl:attribute>
		</xsl:element>
	</xsl:template>
-->
<!-- 
HART device type
-->
	<xsl:template match="mtdevice:IdTypeID">
		<xsl:param name="value" select="@mtident:deviceTypeID"/>
		<xsl:element name="IdTypeID">
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">Gateway ID</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:IdTypeID">
		<xsl:element name="IdTypeID">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:deviceTypeID"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Gateway ID</xsl:attribute>
		</xsl:element>
	</xsl:template>

<!-- 
HART software revision
-->
	<xsl:template match="mtdevice:IdSoftwareRevision">
		<xsl:param name="value" select="@mtident:softwareRevision"/>
		<xsl:element name="IdSoftwareRevision">
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">Device ID(s)</xsl:attribute>
		</xsl:element>
	</xsl:template>
		<xsl:template match="mtscan:IdSoftwareRevision">
		<xsl:param name="value" select="@mtident:softwareRevision"/>
		<xsl:element name="IdSoftwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Device ID(s)</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

<!-- 
HART hardware revision
--> 
	<xsl:template match="mtdevice:IdHardwareRevision">
		<xsl:param name="value" select="@mtident:hardwareRevision"/>
		<xsl:element name="IdHardwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Order number</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:IdHardwareRevision">
		<xsl:param name="value" select="@mtident:hardwareRevision"/>
		<xsl:element name="IdHardwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Order number</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

<!-- 
ManufacturerSpecificExtension
--> 
	<xsl:template match="mtdevice:ManufacturerSpecificExtension">
		<xsl:param name="value" select="@mtident:manufacturerSpecificExtension"/>
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">ManufacturerSpecificExtension</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">ManufacturerSpecificExtension</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="mtscan:ManufacturerSpecificExtension">
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">ManufacturerSpecificExtension</xsl:attribute>
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@mtident:manufacturerSpecificExtension"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">ManufacturerSpecificExtension</xsl:attribute>
		</xsl:element>
	</xsl:template>


<!-- 
generate matching information
-->
	<xsl:template name="genMatch">
		<xsl:param name="value"/>
		<xsl:param name="empty"/>
		<xsl:if test="$value!=$empty">
			<xsl:attribute name="ident:value"><xsl:value-of select="$value"/></xsl:attribute>
		</xsl:if>
		<xsl:apply-templates select="mtident:RegExpr"/>
	</xsl:template>
<!-- 
generate regular exressions
-->
	<xsl:template match="mtident:RegExpr">
		<!-- copy pattern info -->
		<xsl:call-template name="genPattern">
			<xsl:with-param name="match" select="@match"/>
			<xsl:with-param name="nomatch" select="@nomatch"/>
		</xsl:call-template>
	</xsl:template>
<!-- 
generate pattern information
-->
	<xsl:template name="genPattern">
		<xsl:param name="match"/>
		<xsl:param name="nomatch"/>
		<xsl:param name="empty"/>
		<xsl:choose>
		<xsl:when test="$match!=$empty">
			<xsl:element name="ident:RegExpr">
				<xsl:attribute name="match"><xsl:value-of select="$match"/></xsl:attribute>
				<xsl:if test="$nomatch!=$empty">
					<xsl:attribute name="nomatch"><xsl:value-of select="$nomatch"/></xsl:attribute>
				</xsl:if>
			</xsl:element>
		</xsl:when>
		<xsl:when test="$nomatch!=$empty">
			<xsl:element name="ident:RegExpr">
				<xsl:attribute name="nomatch"><xsl:value-of select="$nomatch"/></xsl:attribute>
				<xsl:if test="$match!=$empty">
					<xsl:attribute name="match"><xsl:value-of select="$match"/></xsl:attribute>
				</xsl:if>
			</xsl:element>
		</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:transform>