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

<!--  Device identification transformation for SERCOS III 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:sercosident="x-schema:FDTSERCOSIdentSchema.xml"
	xmlns:sercosdevice="x-schema:FDTSERCOSDeviceTypeIdentSchema.xml"
	xmlns:sercosscan="x-schema:FDTSERCOSScanIdentSchema.xml" version="1.0">

  <!--  Version of this file  -->
  <xsl:variable name="FileVersion">
	<xsl:number value="1.0"/>
  </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="//sercosscan:ScanIdentifications"/>
    <xsl:apply-templates select="//sercosdevice:DeviceIdentifications"/>
  </xsl:template>

  <!--  SERCOS III device identification list  -->
  <xsl:template match="sercosdevice:DeviceIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
  &lt;!-- This file is created by FDTSERCOSIdentTransformation.xsl after transformation of SERCOSDeviceIdentInstance.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="sercosdevice:DeviceIdentification"/>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>

  <!-- SERCOS III scan identification list. -->
  <xsl:template match="sercosscan:ScanIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTSERCOSIdentTransformation.xsl after transformation of SERCOSScanIdentInstance.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="sercosscan:ScanIdentification"/>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>

  <!-- SERCOS III device identifcation. -->
  <xsl:template match="sercosdevice:DeviceIdentification">
    <xsl:element name="DeviceIdentification">
      <xsl:attribute name="ident:idDTMSupportLevel">
        <xsl:value-of select="@sercosident:idDTMSupportLevel"/>
      </xsl:attribute>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">SERCOS</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosdevice:IdBusProtocol/@sercosident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>

      <xsl:apply-templates select="sercosdevice:IdManufacturer"/>
      <xsl:apply-templates select="sercosdevice:IdTypeID"/>
      <xsl:apply-templates select="sercosdevice:IdSoftwareVersion"/> 
      <xsl:apply-templates select="sercosdevice:IdHardwareVersion"/>

    </xsl:element>
  </xsl:template>

  <!-- SERCOS III scan identifcation. -->
  <xsl:template match="sercosscan:ScanIdentification">
    <xsl:element name="ScanIdentification">

      <xsl:apply-templates select="fdt:CommunicationError"/>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">SERCOS</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdBusProtocol/@sercosident: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">SERCOS Addresses</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdAddress/@sercosident:sercosAddresses"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdManufacturer">
        <xsl:attribute name="ident:protocolSpecificName">Vendor Code</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdManufacturer/@fdt:manufacturerId"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdTypeID">
        <xsl:attribute name="ident:protocolSpecificName">Vendor Device ID</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdTypeID/@fdt:subDeviceType"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdSoftwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">Software revision</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdSoftwareRevision/@fdt:version"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdHardwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">Hardware revision</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdHardwareRevision/@fdt:version"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdDeviceTag">
        <xsl:attribute name="ident:protocolSpecificName">Device name</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="sercosscan:IdDeviceName/@fdt:name"/>
        </xsl:attribute>
      </xsl:element>

      <xsl:element name="IdSerialNumber">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>

    </xsl:element>
  </xsl:template>


  <!--  Software revision  -->
  <xsl:template match="sercosdevice:IdSoftwareRevision">    
    <xsl:param name="value" select="@fdt:version"/>
    <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>
 
	<xsl:template match="sercosscan:IdSoftwareRevision">
    <xsl:element name="IdSoftwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Software revision</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@sercosscan:IdSoftwareRevision/@fdt:version"/></xsl:attribute>  
    </xsl:element>
  </xsl:template>

  <!--  Hardware revision  -->
  <xsl:template match="sercosdevice:IdHardwareRevision">
    <xsl:param name="value" select="@fdt:version"/>
    <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="sercosscan:IdHardwareRevision">
    <xsl:element name="IdHardwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Hardware revision</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@sercosscan:IdHardwareRevision/@fdt:version"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!--  Manufacturer ID  -->
  <xsl:template match="sercosdevice:IdManufacturer">
    <xsl:param name="value" select="@fdt:manufacturerId"/>
    <xsl:element name="IdManufacturer">
      <xsl:attribute name="ident:protocolSpecificName">Vendor Code</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  

  <!--  Device type ID  -->
  <xsl:template match="sercosdevice:IdTypeID">
    <xsl:param name="value" select="@fdt:subDeviceType"/>
    <xsl:element name="IdTypeID">
      <xsl:attribute name="ident:protocolSpecificName">Vendor Device ID</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </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="sercosident:RegExpr"/>
  </xsl:template>

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