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

<!-- 
FDT: device identification transformation for HART 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:hartident="x-schema:FDTHARTIdentSchema.xml"
		xmlns:hartdevice="x-schema:FDTHARTDeviceTypeIdentSchema.xml"
		xmlns:hartscan="x-schema:FDTHARTScanIdentSchema.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="//hartscan:ScanIdentifications"/>
			<xsl:apply-templates select="//hartdevice:DeviceIdentifications"/>
	</xsl:template>
<!-- 
HART identification list
-->
	<xsl:template match="hartdevice: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="hartdevice:DeviceIdentification"/>
				</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>
<!-- 
HART catalog identifcation
-->
	<xsl:template match="hartdevice:DeviceIdentification">
			<xsl:element name="DeviceIdentification">
				<xsl:attribute name="ident:idDTMSupportLevel">
					<xsl:value-of select="@hartident:idDTMSupportLevel"/>
				</xsl:attribute>
				<xsl:apply-templates select="hartdevice:IdBusProtocol"/>
				<xsl:apply-templates select="hartdevice:IdBusProtocolVersion"/>
				<xsl:apply-templates select="hartdevice:IdManufacturer"/>
				<xsl:apply-templates select="hartdevice:IdTypeID"/>
				<xsl:apply-templates select="hartdevice:IdSoftwareRevision"/>
				<xsl:apply-templates select="hartdevice:IdHardwareRevision"/>
				<xsl:element name="IdValues">
					<xsl:apply-templates select="hartdevice:DeviceCommandRevisionLevel"/>
					<xsl:apply-templates select="hartdevice:DeviceFlag"/>
					<xsl:apply-templates select="hartdevice:ManufacturerSpecificExtension"/>
				</xsl:element>
			</xsl:element>
	</xsl:template>
<!-- 
HART scan list
-->
	<xsl:template match="hartscan: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="hartscan:ScanIdentification"/>
				</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>


<!-- 
HART scan identifcation
-->
	<xsl:template match="hartscan:ScanIdentification">
			<xsl:element name="ScanIdentification">
				<xsl:apply-templates select="@configuredState" />
				<xsl:apply-templates select="fdt:CommunicationError"/>
				<xsl:apply-templates select="hartscan:IdBusProtocol"/>
				<xsl:apply-templates select="hartscan:IdBusProtocolVersion"/>
				<xsl:apply-templates select="hartscan:IdAddress"/>			
				<xsl:apply-templates select="hartscan:IdManufacturer"/>
				<xsl:apply-templates select="hartscan:IdTypeID"/>
				<xsl:apply-templates select="hartscan:IdSoftwareRevision"/>
				<xsl:apply-templates select="hartscan:IdHardwareRevision"/>
				<xsl:apply-templates select="hartscan:IdTag"/>
				<xsl:apply-templates select="hartscan:IdSerialNumber"/>
				<xsl:element name="IdValues">
					<xsl:apply-templates select="hartscan:DeviceCommandRevisionLevel"/>
					<xsl:apply-templates select="hartscan:DeviceFlag"/>
					<xsl:apply-templates select="hartscan: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="hartscan:IdTag">
		<xsl:element name="IdDeviceTag">
			<xsl:attribute name="ident:value"><xsl:value-of select="@hartident: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="hartscan:IdSerialNumber">
		<xsl:element name="IdSerialNumber">
			<xsl:attribute name="ident:value"><xsl:value-of select="@hartident:deviceID"/></xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Device Identification Number</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
HartScanIdentification: busaddress, just copy
-->
	<xsl:template match="hartscan:IdAddress">
		<xsl:element name="IdAddress">
			<xsl:attribute name="ident:value"><xsl:value-of select="@hartident: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="hartdevice:DeviceCommandRevisionLevel">
		<xsl:param name="value" select="@hartident: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="hartscan:DeviceCommandRevisionLevel">
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">DeviceCommandRevisionLevel</xsl:attribute>
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:deviceCommandRevisionLevel"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Device Revision Level</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
HartScanIdentification: DeviceFlag, saved as protocol specific value
-->
	<xsl:template match="hartdevice:DeviceFlag">
		<xsl:param name="value" select="@hartident:deviceFlag"/>
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">DeviceFlag</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
			<xsl:attribute name="ident:protocolSpecificName">Flags</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="hartscan:DeviceFlag">
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">DeviceFlag</xsl:attribute>
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:deviceFlag"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Flags</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
Hart Manufacaturer
-->
	<xsl:template match="hartdevice:IdManufacturer">
		<xsl:param name="manid" select="@hartident: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="hartscan:IdManufacturer">
		<xsl:element name="IdManufacturer">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:manufacturerIdentificationCode"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Manufacturer Identification Code</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
DevId:IdBusProtocol
-->
	<xsl:template match="hartdevice:IdBusProtocol">
		<xsl:param name="manid" select="@hartident: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">HART</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="hartscan:IdBusProtocol">
		<xsl:element name="IdBusProtocol">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:busProtocol"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">HART</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
DevId:IdBusProtocolVersion
-->
	<xsl:template match="hartdevice:IdBusProtocolVersion">
		<xsl:param name="manid" select="@hartident: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">HART Revision</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="hartscan:IdBusProtocolVersion">
		<xsl:element name="IdBusProtocolVersion">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:universalCommandRevisionLevel"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">HART Revision</xsl:attribute>
		</xsl:element>
	</xsl:template>
<!-- 
HART device type
-->
	<xsl:template match="hartdevice:IdTypeID">
		<xsl:param name="value" select="@hartident: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">Device Type Code</xsl:attribute>
		</xsl:element>
	</xsl:template>
	<xsl:template match="hartscan:IdTypeID">
		<xsl:element name="IdTypeID">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident:deviceTypeID"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Device Type Code</xsl:attribute>
		</xsl:element>
	</xsl:template>

<!-- 
HART software revision
-->
	<xsl:template match="hartdevice:IdSoftwareRevision">
		<xsl:param name="value" select="@hartident: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">Software Revision</xsl:attribute>
		</xsl:element>
	</xsl:template>
		<xsl:template match="hartscan:IdSoftwareRevision">
		<xsl:param name="value" select="@hartident:softwareRevision"/>
		<xsl:element name="IdSoftwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Software Revision</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="hartdevice:IdHardwareRevision">
		<xsl:param name="value" select="@hartident:hardwareRevision"/>
		<xsl:element name="IdHardwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Hardware Revision</xsl:attribute>
			<xsl:call-template name="genMatch">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>
	<xsl:template match="hartscan:IdHardwareRevision">
		<xsl:param name="value" select="@hartident:hardwareRevision"/>
		<xsl:element name="IdHardwareRevision">
			<xsl:attribute name="ident:protocolSpecificName">Hardware Revision</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="hartdevice:ManufacturerSpecificExtension">
		<xsl:param name="value" select="@hartident: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="hartscan:ManufacturerSpecificExtension">
		<xsl:element name="IdValue">
			<xsl:attribute name="ident:name">ManufacturerSpecificExtension</xsl:attribute>
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@hartident: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="hartident:RegExpr"/>
	</xsl:template>
<!-- 
generate regular exressions
-->
	<xsl:template match="hartident: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>