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

<!-- Device identification transformation for ProfinetIO identification files. -->
<xsl:stylesheet 
	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:pnioident="x-schema:FDTProfinetIOIdentSchema.xml"
	xmlns:pniodevice="x-schema:FDTProfinetIODeviceTypeIdentSchema.xml"
	xmlns:pnioscan="x-schema:FDTProfinetIOScanIdentSchema.xml" version="2.0">

  <!-- Version of this file. -->
  <xsl:variable name="FileVersion">
    <xsl:number value="1.00"/>
  </xsl:variable>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <!-- Transform device root or scan identification. -->
  <xsl:template match="/">
    <xsl:apply-templates select="//pnioscan:ScanIdentifications"/>
    <xsl:apply-templates select="//pniodevice:DeviceIdentifications"/>
  </xsl:template>

  <!-- ProfinetIO catalog identification list. -->
  <xsl:template match="pniodevice:DeviceIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTProfinetIOIdentTransformation.xsl after transformation of ProfinetIODeviceIdentInstance.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="pniodevice:DeviceIdentification_DCP"/>
      <xsl:apply-templates select="pniodevice:DeviceIdentification_IM"/>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>

  <!-- ProfinetIO scan identification list. -->
  <xsl:template match="pnioscan:ScanIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTProfinetIOIdentTransformation.xsl after transformation of ProfinetIOScanIdentInstance.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="pnioscan:ScanIdentification_DCP"/>
      <xsl:apply-templates select="pnioscan:ScanIdentification_IM"/>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>

  <!-- ProfinetIO DCP catalog identifcation. -->
  <xsl:template match="pniodevice:DeviceIdentification_DCP">
    <xsl:element name="DeviceIdentification">
      <xsl:attribute name="ident:idDTMSupportLevel">
        <xsl:value-of select="@pnioident:idDTMSupportLevel"/>
      </xsl:attribute>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">ProfinetIO DCP</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="pniodevice:IdBusProtocol/@pnioident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="pniodevice:IdManufacturer"/>
      <xsl:apply-templates select="pniodevice:IdTypeID"/>
      <xsl:element name="IdSoftwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdHardwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdValues">
        <xsl:apply-templates select="pniodevice:IdDeviceRole"/>
        <xsl:apply-templates select="pniodevice:ManufacturerSpecificExtension"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- ProfinetIO DCP scan identifcation. -->
  <xsl:template match="pnioscan:ScanIdentification_DCP">
    <xsl:element name="ScanIdentification">
      <xsl:apply-templates select="@configuredState"/>
      <xsl:apply-templates select="fdt:CommunicationError"/>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">ProfinetIO DCP</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="pnioscan:IdBusProtocol/@pnioident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdAddress">
        <xsl:attribute name="ident:protocolSpecificName">Address</xsl:attribute>
        <xsl:attribute name="ident:value"><xsl:value-of select="pnioscan:IdAddress/@pnioident:nameOfStation"/>;<xsl:value-of select="pnioscan:IdAddress/@pnioident:ipAddress"/>;<xsl:value-of select="pnioscan:IdAddress/@pnioident:ipSubnetMask"/>;<xsl:value-of select="pnioscan:IdAddress/@pnioident:ipDefaultGateway"/>;<xsl:value-of select="pnioscan:IdAddress/@pnioident:macAddress"/></xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="pnioscan:IdManufacturer"/>
      <xsl:apply-templates select="pnioscan:IdTypeID"/>
      <xsl:element name="IdSoftwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdHardwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdDeviceTag">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdSerialNumber">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdValues">
        <xsl:apply-templates select="pnioscan:IdDeviceRole"/>
        <xsl:apply-templates select="pnioscan:ManufacturerSpecificExtension"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- ProfinetIO IM catalog identifcation. -->
  <xsl:template match="pniodevice:DeviceIdentification_IM">
    <xsl:element name="DeviceIdentification">
      <xsl:attribute name="ident:idDTMSupportLevel">
        <xsl:value-of select="@pnioident:idDTMSupportLevel"/>
      </xsl:attribute>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">ProfinetIO IM</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="pniodevice:IdBusProtocol/@pnioident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="pniodevice:IdManufacturer"/>
      <xsl:apply-templates select="pniodevice:IdTypeID"/>
      <xsl:apply-templates select="pniodevice:IdSoftwareRevision"/>
      <xsl:apply-templates select="pniodevice:IdHardwareRevision"/>
      <xsl:element name="IdValues">
        <xsl:apply-templates select="pniodevice:IdDeviceRole"/>
        <xsl:apply-templates select="pniodevice:ManufacturerSpecificExtension"/>
        <xsl:apply-templates select="pniodevice:ProfileID"/>
        <xsl:apply-templates select="pniodevice:ProfileSpecificType"/>
        <xsl:apply-templates select="pniodevice:IdOrder"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- ProfinetIO IM scan identifcation. -->
  <xsl:template match="pnioscan:ScanIdentification_IM">
    <xsl:element name="ScanIdentification">
      <xsl:apply-templates select="@configuredState"/>
      <xsl:apply-templates select="fdt:CommunicationError"/>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">ProfinetIO IM</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="pnioscan:IdBusProtocol/@pnioident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="pnioscan:IdAddress"/>
      <xsl:apply-templates select="pnioscan:IdManufacturer"/>
      <xsl:apply-templates select="pnioscan:IdTypeID"/>
      <xsl:apply-templates select="pnioscan:IdSoftwareRevision"/>
      <xsl:apply-templates select="pnioscan:IdHardwareRevision"/>
      <xsl:element name="IdDeviceTag">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="pnioscan:IdSerialNumber"/>
      <xsl:element name="IdValues">
        <xsl:apply-templates select="pnioscan:IdDeviceRole"/>
        <xsl:apply-templates select="pnioscan:ManufacturerSpecificExtension"/>
        <xsl:apply-templates select="pnioscan:ProfileID"/>
        <xsl:apply-templates select="pnioscan:ProfileSpecificType"/>
        <xsl:apply-templates select="pnioscan:IdOrder"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- Device address. -->
  <xsl:template match="pnioscan:IdAddress">
    <xsl:element name="IdAddress">
      <xsl:attribute name="ident:protocolSpecificName">Address</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@pnioident:nameOfStation"/>;<xsl:value-of select="@pnioident:ipAddress"/>;<xsl:value-of select="@pnioident:ipSubnetMask"/>;<xsl:value-of select="@pnioident:ipDefaultGateway"/>;<xsl:value-of select="@pnioident:macAddress"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Serial number. -->
  <xsl:template match="pnioscan:IdSerialNumber">
    <xsl:element name="IdSerialNumber">
      <xsl:attribute name="ident:protocolSpecificName">IM_Serial_Number</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="pnioscan:IdSerialNumber/@pnioident:serialNumber"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Manufacturer ID. -->
  <xsl:template match="pniodevice:IdManufacturer">
    <xsl:param name="value" select="@fdt:manufacturerId"/>
    <xsl:element name="IdManufacturer">
      <xsl:attribute name="ident:protocolSpecificName">DeviceVendorValue</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:IdManufacturer">
    <xsl:element name="IdManufacturer">
      <xsl:attribute name="ident:protocolSpecificName">DeviceVendorValue</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@fdt:manufacturerId"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Device type ID. -->
  <xsl:template match="pniodevice:IdTypeID">
    <xsl:param name="value" select="@fdt:deviceTypeId"/>
    <xsl:element name="IdTypeID">
      <xsl:attribute name="ident:protocolSpecificName">DeviceIdentNumber</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:IdTypeID">
    <xsl:element name="IdTypeID">
      <xsl:attribute name="ident:protocolSpecificName">DeviceIdentNumber</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@fdt:deviceTypeId"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Device role. -->
  <xsl:template match="pniodevice:IdDeviceRole">
    <xsl:param name="value" select="@pnioident:deviceRole"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">DeviceRoleDetails</xsl:attribute>
      <xsl:attribute name="ident:name">Device Role</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:IdDeviceRole">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">DeviceRoleDetails</xsl:attribute>
      <xsl:attribute name="ident:name">Device Role</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@pnioident:deviceRole"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Manufacturer specific extension. -->
  <xsl:template match="pniodevice:ManufacturerSpecificExtension">
    <xsl:param name="value" select="@pnioident:manufacturerSpecificExtension"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">ManufacturerSpecificString</xsl:attribute>
      <xsl:attribute name="ident:name">Manufacturer Specific Extension</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:ManufacturerSpecificExtension">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">ManufacturerSpecificString</xsl:attribute>
      <xsl:attribute name="ident:name">Manufacturer Specific Extension</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@pnioident:manufacturerSpecificExtension"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Software revision. -->
  <xsl:template match="pniodevice:IdSoftwareRevision">
    <xsl:param name="value" select="@fdt:firmwareRevision"/>
    <xsl:element name="IdSoftwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">IM_Software_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="pnioscan:IdSoftwareRevision">
    <xsl:element name="IdSoftwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">IM_Software_Revision</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@fdt:firmwareRevision"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Hardware revision. -->
  <xsl:template match="pniodevice:IdHardwareRevision">
    <xsl:param name="value" select="@fdt:hardwareRevision"/>
    <xsl:element name="IdHardwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">IM_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="pnioscan:IdHardwareRevision">
    <xsl:element name="IdHardwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">IM_Hardware_Revision</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@fdt:hardwareRevision"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Profile ID. -->
  <xsl:template match="pniodevice:ProfileID">
    <xsl:param name="value" select="@pnioident:profileID"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Profile_ID</xsl:attribute>
      <xsl:attribute name="ident:name">Profile ID</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:ProfileID">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Profile_ID</xsl:attribute>
      <xsl:attribute name="ident:name">Profile ID</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@pnioident:profileID"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Profile specific type. -->
  <xsl:template match="pniodevice:ProfileSpecificType">
    <xsl:param name="value" select="@pnioident:profileID"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Profile_Specific_Type</xsl:attribute>
      <xsl:attribute name="ident:name">Profile Specific Type</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:ProfileSpecificType">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Profile_Specific_Type</xsl:attribute>
      <xsl:attribute name="ident:name">Profile Specific Type</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@pnioident:profileSpecificType"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Order ID. -->
  <xsl:template match="pniodevice:IdOrder">
    <xsl:param name="value" select="@pnioident:orderId"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Order_ID</xsl:attribute>
      <xsl:attribute name="ident:name">Order ID</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="pnioscan:IdOrder">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">IM_Order_ID</xsl:attribute>
      <xsl:attribute name="ident:name">Order ID</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@pnioident:orderID"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Configured attribute. -->
  <xsl:template match="@configuredState">
    <xsl:attribute name="configuredState">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <!-- Communication error. -->
  <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>

  <!-- 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="pnioident:RegExpr"/>
  </xsl:template>

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