<!-- XSLT transformation to transform step element into entities -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:ms="urn:schemas-microsoft-com:xslt"
  >
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <xsl:if test="dialogue">
      <Interact_Out>
        <xsl:attribute name="Reference_Number">
          <xsl:value-of select="dialogue/reference" />
        </xsl:attribute>
        <Entities>
          <xsl:apply-templates select="dialogue/step[@id = 'WZ_ODATA']/group[@id != 'RecordTemplate']"/>
        </Entities>
        <RecordTemplate>
          <xsl:apply-templates select="dialogue/step[@id = 'WZ_ODATA']/group[@id = 'RecordTemplate']"/>
        </RecordTemplate>
        <AdditionalData/>
      </Interact_Out>
    </xsl:if>
  </xsl:template>

  <xsl:template match="group[@id != 'RecordTemplate']">
    <xsl:if test="@id != 'RecordTemplate'">
      <xsl:if test=". != ''">
        <Entity>
          <xsl:attribute name="EntitySet">
            <xsl:value-of select="@label" />
          </xsl:attribute>
          <xsl:attribute name="Id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:apply-templates select="element"/>
        </Entity>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template match="group[@id = 'RecordTemplate']">
    <xsl:if test="@id = 'RecordTemplate'">
      <xsl:apply-templates select="element"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="element">
    <xsl:if test=". != ''">
      <Property>
        <xsl:attribute name="Name">
          <xsl:value-of select="@label" />
        </xsl:attribute>
        <xsl:choose>
          <xsl:when test="string-length(.) = 19 and substring(.,5,1)='-' and translate(.,'0123456789- :T','')=''">
            <xsl:attribute name="Type">DateTime</xsl:attribute>
            <xsl:value-of select="."/>
          </xsl:when>
          <xsl:when test="string-length(.) = 10 and substring(.,5,1)='-' and translate(.,'0123456789- :T','')='' ">
            <xsl:attribute name="Type">DateTime</xsl:attribute>
            <xsl:value-of select="."/>
          </xsl:when>                              
          <xsl:otherwise>
            <xsl:attribute name="Type">string</xsl:attribute>
            <xsl:value-of select="."/>            
          </xsl:otherwise>
        </xsl:choose>
      </Property>
    </xsl:if>
  </xsl:template>


  <xsl:template match="element[@label='File']|element[@label='Name']|element[@label='Records']">
    <xsl:if test=". != ''">
      <EntityRef>
        <xsl:attribute name="Name">
          <xsl:value-of select="@label" />
        </xsl:attribute>
        <xsl:attribute name="IdRef">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </EntityRef >
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>