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

<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:cipident="x-schema:FDTCIPIdentSchema.xml"
	       xmlns:cipscanident="x-schema:FDTCIPScanIdentSchema.xml"
	       xmlns:cipdeviceident="x-schema:FDTCIPDeviceTypeIdentSchema.xml"
	       xmlns:cip="x-schema:DTMCIPDataTypeSchema.xml"
	       xmlns:cipscan="x-schema:DTMTopologyScanSchema.xml"
	       version="2.0">
	<xsl:variable name="FileVersion">
		<xsl:number value="1.30"/>
	</xsl:variable>
	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
  
	<xsl:template match="/">	
		<xsl:apply-templates select="//cipscanident:ScanIdentifications" />
		<xsl:apply-templates select="//cipdeviceident:DeviceIdentifications" />
	</xsl:template>
  
	<xsl:template match="cipscanident:ScanIdentifications">
		<xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
			&lt;!-- This file is created by FDTCIPIdentTransformation.xsl after transformation of CIPDTMScanIdentificationInstance.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="cipscanident:ScanIdentification" />
		</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>
  
	<!--CIP scan ident-->
	<xsl:template match="cipscanident:ScanIdentification">
		<xsl:element name="ScanIdentification">
			<xsl:element name="IdBusProtocol">
				<xsl:attribute name="ident:value"><xsl:value-of select="cipscanident:IdBusProtocol/@cipident:busProtocol"/></xsl:attribute>
				<xsl:attribute name="ident:protocolSpecificName">CIP</xsl:attribute>
			</xsl:element>
			<xsl:element name="IdBusProtocolVersion">
				<xsl:attribute name="ident:value">1</xsl:attribute>
				<xsl:attribute name="ident:protocolSpecificName">CIP Revision</xsl:attribute>
			</xsl:element>
			<xsl:apply-templates select="cip:CIPDevice/cip:CIPPath" />
			<xsl:apply-templates select="cip:CIPDevice/cip:CIPDeviceIdentity" />
		</xsl:element>
	</xsl:template>
  
	<xsl:template match="cipdeviceident:DeviceIdentifications">
		<xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
			&lt;!-- This file is created by FDTCIPIdentTransformation.xsl after transformation of CIPDTMDeviceIdentificationInstance.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="cipdeviceident:DeviceIdentification" />
		</xsl:element>
		<xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
	</xsl:template>
  
	<!-- DeviceIdentification-->
	<xsl:template match="cipdeviceident:DeviceIdentification">
		<xsl:element name="DeviceIdentification">
			<xsl:attribute name="ident:idDTMSupportLevel">
				<xsl:value-of select="@cipident:idDTMSupportLevel" />
			</xsl:attribute>
			<xsl:element name="IdBusProtocol">
				<xsl:attribute name="ident:value"><xsl:value-of select="cipdeviceident:IdBusProtocol/@cipident:busProtocol"/></xsl:attribute>
				<xsl:attribute name="ident:protocolSpecificName">CIP</xsl:attribute>
			</xsl:element>
			<xsl:element name="IdBusProtocolVersion">
				<xsl:attribute name="ident:value">1</xsl:attribute>
				<xsl:attribute name="ident:protocolSpecificName">CIP Revision</xsl:attribute>
			</xsl:element>
			
			<xsl:element name="IdManufacturer">
				<xsl:attribute name="ident:protocolSpecificName">Manufacturer</xsl:attribute>
				<xsl:attribute name="ident:value">
					<xsl:value-of select="cip:CIPDeviceIdentity/@vendorID" />
				</xsl:attribute>
				<xsl:apply-templates select="cipdeviceident:IdVendorID" />	
			</xsl:element>

			<xsl:element name="IdTypeID">
				<xsl:attribute name="ident:protocolSpecificName">DeviceType</xsl:attribute>
				<xsl:attribute name="ident:value">
					<xsl:value-of select="cip:CIPDeviceIdentity/@deviceType" />-<xsl:value-of select="cip:CIPDeviceIdentity/@productCode"/>
				</xsl:attribute>
				<xsl:apply-templates select="cipdeviceident:IdDeviceType" />
			</xsl:element>

			<xsl:element name="IdSoftwareRevision">
				<xsl:attribute name="ident:protocolSpecificName">Software Revision</xsl:attribute>
				<xsl:attribute name="ident:value">
					<xsl:value-of select="cip:CIPDeviceIdentity/@majorRevision" />.<xsl:value-of select="cip:CIPDeviceIdentity/@minorRevision"/>
				</xsl:attribute>
				<xsl:apply-templates select="cipdeviceident:IdRevision" />
			</xsl:element>

			<xsl:element name="IdHardwareRevision">
				<xsl:attribute name="ident:protocolSpecificName">Hardware Revision</xsl:attribute>
				<xsl:attribute name="ident:value">
					<xsl:value-of select="cip:CIPDeviceIdentity/@majorRevision" />.<xsl:value-of select="cip:CIPDeviceIdentity/@minorRevision"/>
				</xsl:attribute>
				<xsl:apply-templates select="cipdeviceident:IdRevision" />
			</xsl:element>		
		</xsl:element>
	</xsl:template>
  
	<!--CIP device data-->
	<xsl:template match="cipscanident:ScanIdentification/cip:CIPDevice/cip:CIPDeviceIdentity">
		<xsl:element name="IdManufacturer">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@vendorID" />
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">DeviceNet Revision</xsl:attribute>
		</xsl:element>
		
		<xsl:element name="IdTypeID">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@deviceType" />-<xsl:value-of select="@productCode"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">DeviceType-ProductCode</xsl:attribute>
		</xsl:element>
		
		<xsl:element name="IdSoftwareRevision">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@majorRevision" />.<xsl:value-of select="@minorRevision"/>
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">MajorRevision.MinorRevision</xsl:attribute>
		</xsl:element>
	
		<xsl:element name="IdHardwareRevision">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@majorRevision" />.<xsl:value-of select="@minorRevision" />
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">MajorRevision.MinorRevision</xsl:attribute>
		</xsl:element>
	
		<xsl:element name="IdDeviceTag">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@productName" />
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">Product Name</xsl:attribute>
		</xsl:element>
	
		<xsl:element name="IdSerialNumber">
			<xsl:attribute name="ident:value">
				<xsl:value-of select="@serialNumber" />
			</xsl:attribute>
			<xsl:attribute name="ident:protocolSpecificName">SerialNumber</xsl:attribute>
		</xsl:element>
	</xsl:template>
   
	<!--TEMPLATES-->
	<!--CIP device address-->
	<xsl:template match="cip:CIPPath">
		<xsl:element name="IdAddress">
			<xsl:choose>
				<xsl:when test="cip:CIPNodeID/cip:ShortIdentifier">
					<xsl:attribute name="ident:value">
						<xsl:value-of select="cip:CIPNodeID/cip:ShortIdentifier/@shortIdentifier" />
					</xsl:attribute>
					<xsl:attribute name="ident:protocolSpecificName">Short Identifier</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="ident:value">
						<xsl:value-of select="cip:CIPNodeID/cip:ExtendedIdentifier/@extendedIdentifier" />
					</xsl:attribute>
					<xsl:attribute name="ident:protocolSpecificName">Extended Identifier</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
  
	<xsl:template match="cipdeviceident:IdVendorID">
		<xsl:call-template name="genMatch">
		</xsl:call-template>
	</xsl:template>

	<xsl:template match="cipdeviceident:IdDeviceType">
		<xsl:call-template name="genMatch">
		</xsl:call-template>
	</xsl:template>

	<xsl:template match="cipdeviceident:IdRevision">
		<xsl:call-template name="genMatch">
		</xsl:call-template>
	</xsl:template>
	
	<!-- Generate matching information-->
	<xsl:template name="genMatch">
		<xsl:apply-templates select="cipident:RegExpr" />	
	</xsl:template>

	<!-- Generate regular exressions-->
	<xsl:template match="cipident: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>