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

<!-- Device identification transformation for IO-Link 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:iolinkident="x-schema:FDTIOLinkIdentSchema.xml"
	xmlns:iolinkdevice="x-schema:FDTIOLinkDeviceTypeIdentSchema.xml"
	xmlns:iolinkscan="x-schema:FDTIOLinkScanIdentSchema.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="no" indent="yes"/>

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

  <!-- IO-Link catalog identification list. -->
  <xsl:template match="iolinkdevice:DeviceIdentifications">
    <xsl:comment>This file is created by FDTIOLinkIdentTransformation.xsl after transformation of IOLinkDTMDeviceIdentificationInstance.xml.</xsl:comment>
    <xsl:element name="FDT" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:element name="DeviceIdentifications" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
        <xsl:apply-templates select="iolinkdevice:DeviceIdentification"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- IO-Link scan identification list. -->
  <xsl:template match="iolinkscan:ScanIdentifications">
    <xsl:comment>This file is created by FDTIOLinkIdentTransformation.xsl after transformation of IOLinkDTMScanIdentificationInstance.xml.</xsl:comment>
    <xsl:element name="FDT" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:element name="ScanIdentifications" namespace="x-schema:DTMScanIdentSchema.xml">
        <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="iolinkscan:ScanIdentification"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- IO-Link catalog identifcation. -->
  <xsl:template match="iolinkdevice:DeviceIdentification">
    <xsl:element name="DeviceIdentification" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:idDTMSupportLevel">
        <xsl:value-of select="@iolinkident:idDTMSupportLevel"/>
      </xsl:attribute>

      <xsl:element name="IdBusProtocol" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>

      <xsl:apply-templates select="iolinkdevice:IdBusProtocolVersion"/>
      <xsl:apply-templates select="iolinkdevice:IdManufacturer"/>
      <xsl:apply-templates select="iolinkdevice:IdTypeID"/>

      <xsl:if test="not(iolinkdevice:IdSoftwareRevision)">
        <xsl:element name="IdSoftwareRevision" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
          <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:apply-templates select="iolinkdevice:IdSoftwareRevision"/>

      <xsl:if test="not(iolinkdevice:IdHardwareRevision)">
        <xsl:element name="IdHardwareRevision" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
          <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:apply-templates select="iolinkdevice:IdHardwareRevision"/>

      <xsl:element name="IdValues" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
        <xsl:apply-templates select="iolinkdevice:FunctionId"/>
        <xsl:apply-templates select="iolinkdevice:ProductId"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- IO-Link scan identifcation. -->
  <xsl:template match="iolinkscan:ScanIdentification">
    <xsl:element name="ScanIdentification" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:apply-templates select="@configuredState"/>
      <xsl:apply-templates select="fdt:CommunicationError"/>

      <xsl:element name="IdBusProtocol" namespace="x-schema:DTMScanIdentSchema.xml">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        <xsl:attribute name="ident:value"/>
      </xsl:element>

      <xsl:apply-templates select="iolinkscan:IdBusProtocolVersion"/>

      <xsl:element name="IdAddress" namespace="x-schema:DTMScanIdentSchema.xml">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        <xsl:attribute name="ident:value"/>
      </xsl:element>

      <xsl:apply-templates select="iolinkscan:IdManufacturer"/>
      <xsl:apply-templates select="iolinkscan:IdTypeID"/>

      <xsl:if test="not(iolinkscan:IdSoftwareRevision)">
        <xsl:element name="IdSoftwareRevision" namespace="x-schema:DTMScanIdentSchema.xml">
          <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:apply-templates select="iolinkscan:IdSoftwareRevision"/>

      <xsl:if test="not(iolinkscan:IdHardwareRevision)">
        <xsl:element name="IdHardwareRevision" namespace="x-schema:DTMScanIdentSchema.xml">
          <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:apply-templates select="iolinkscan:IdHardwareRevision"/>

      <xsl:element name="IdDeviceTag" namespace="x-schema:DTMScanIdentSchema.xml">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:element name="IdSerialNumber" namespace="x-schema:DTMScanIdentSchema.xml">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>

      <xsl:element name="IdValues" namespace="x-schema:DTMScanIdentSchema.xml">
        <xsl:apply-templates select="iolinkscan:FunctionId"/>
        <xsl:apply-templates select="iolinkscan:ProductId"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- IO-Link Revision -->
  <xsl:template match="iolinkdevice:IdBusProtocolVersion">
    <xsl:param name="value" select="@iolinkident:iolinkRevision"/>
    <xsl:element name="IdBusProtocolVersion" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">IO-Link 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="iolinkscan:IdBusProtocolVersion">
    <xsl:element name="IdBusProtocolVersion" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">IO-Link Revision</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@iolinkident:iolinkRevision"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Vendor Id -->
  <xsl:template match="iolinkdevice:IdManufacturer">
    <xsl:param name="value" select="@fdt:manufacturerId"/>
    <xsl:element name="IdManufacturer" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Vendor 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="iolinkscan:IdManufacturer">
    <xsl:element name="IdManufacturer" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Vendor Id</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@fdt:manufacturerId"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Device Id -->
  <xsl:template match="iolinkdevice:IdTypeID">
    <xsl:param name="value" select="@fdt:deviceTypeId"/>
    <xsl:element name="IdTypeID" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Device 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="iolinkscan:IdTypeID">
    <xsl:element name="IdTypeID" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Device Id</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@fdt:deviceTypeId"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Firmware Revision -->
  <xsl:template match="iolinkdevice:IdSoftwareRevision">
    <xsl:param name="value" select="@iolinkident:firmwareRevision"/>
    <xsl:element name="IdSoftwareRevision" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Firmware 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="iolinkscan:IdSoftwareRevision">
    <xsl:element name="IdSoftwareRevision" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Firmware Revision</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@iolinkident:firmwareRevision"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Hardware Revision -->
  <xsl:template match="iolinkdevice:IdHardwareRevision">
    <xsl:param name="value" select="@iolinkident:hardwareRevision"/>
    <xsl:element name="IdHardwareRevision" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <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="iolinkscan:IdHardwareRevision">
    <xsl:element name="IdHardwareRevision" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Hardware Revision</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@iolinkident:hardwareRevision"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Function Id -->
  <xsl:template match="iolinkdevice:FunctionId">
    <xsl:param name="value" select="@iolinkident:functionId"/>
    <xsl:element name="IdValue" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Function Id</xsl:attribute>
      <xsl:attribute name="ident:name">FunctionId</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="iolinkscan:FunctionId">
    <xsl:element name="IdValue" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Function Id</xsl:attribute>
      <xsl:attribute name="ident:name">FunctionId</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@iolinkident:functionId"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Product Id -->
  <xsl:template match="iolinkdevice:ProductId">
    <xsl:param name="value" select="@iolinkident:productId"/>
    <xsl:element name="IdValue" namespace="x-schema:DTMDeviceTypeIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Product Id</xsl:attribute>
      <xsl:attribute name="ident:name">ProductId</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="iolinkscan:ProductId">
    <xsl:element name="IdValue" namespace="x-schema:DTMScanIdentSchema.xml">
      <xsl:attribute name="ident:protocolSpecificName">Product Id</xsl:attribute>
      <xsl:attribute name="ident:name">ProductId</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@iolinkident:productId"/>
      </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="iolinkident:RegExpr"/>
  </xsl:template>

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