<!-- XSLT transformation to transform step element into entities -->
<xsl:stylesheet version="2.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'">
      <Entity>
        <xsl:if test=". != ''">
          <xsl:attribute name="EntitySet">
            <xsl:if test="starts-with(@label,'Objects_')">

              <xsl:variable name="lastIndex">
                <xsl:call-template name="last-index-of">
                  <xsl:with-param name="txt" select="@label"/>
                  <xsl:with-param name="delimiter" select="'_'"></xsl:with-param>
                </xsl:call-template>
              </xsl:variable>
              <xsl:text>Objects/Som.Custom.</xsl:text>
              <xsl:value-of select="substring(@label, $lastIndex+1, 50)"/>              
            </xsl:if>
            <xsl:if test="not(starts-with(@label,'Objects_')) and contains(@label,'Int64')">
              <xsl:value-of select="substring-before(@label,'Int64')" />
              <xsl:text>Int64</xsl:text>
              <xsl:value-of select="translate(substring-after(@label,'Int64'),'012345789','')" />
            </xsl:if>
            <xsl:if test="not(starts-with(@label,'Objects_')) and not(contains(@label,'Int64'))">
              <xsl:value-of select="translate(@label,'0123456789','')" />
            </xsl:if>
          </xsl:attribute>
          <xsl:attribute name="Id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:apply-templates select="element"/>
        </xsl:if>
        <xsl:apply-templates select="group[@id = 'References']"/>
      </Entity>
    </xsl:if>
  </xsl:template>

  <xsl:template match="group[@id != 'RecordTemplate']/group[@id = 'References']">
    <xsl:if test="@id = 'References'">
      <xsl:apply-templates select="element"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="group[@id = 'RecordTemplate']">
    <xsl:if test="@id = 'RecordTemplate'">
      <xsl:apply-templates select="element"/>
      <xsl:apply-templates select="group[@id = 'References']"/>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="group[@id = 'RecordTemplate']/group[@id = 'References']">
    <xsl:if test="@id = 'References'">
      <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:attribute name="Type">
          <xsl:choose>
            <xsl:when test="starts-with(.,'{')">json</xsl:when>
            <xsl:when test="@type='string'">string</xsl:when>
            <xsl:when test="@type='date'">datetime</xsl:when>
            <xsl:when test="@type='dateTime'">datetime</xsl:when>
            <xsl:when test="@type='integer'">int</xsl:when>
            <xsl:when test="@type='boolean'">boolean</xsl:when>
            <xsl:when test="@type='decimal'">string</xsl:when>
            <xsl:when test="@type='int'">string</xsl:when>
          </xsl:choose>          
        </xsl:attribute>
        <xsl:value-of select="."/>
      </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>

  <!--
Source - https://stackoverflow.com/a
Posted by Mads Hansen, modified by community. See post 'Timeline' for change history
Retrieved 2026-01-22, License - CC BY-SA 3.0
-->

  <xsl:template name="last-index-of">
    <xsl:param name="txt"/>
    <xsl:param name="remainder" select="$txt"/>
    <xsl:param name="delimiter" select="' '"/>

    <xsl:choose>
      <xsl:when test="contains($remainder, $delimiter)">
        <xsl:call-template name="last-index-of">
          <xsl:with-param name="txt" select="$txt"/>
          <xsl:with-param name="remainder" select="substring-after($remainder, $delimiter)"/>
          <xsl:with-param name="delimiter" select="$delimiter"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="lastIndex" select="string-length(substring($txt, 1, string-length($txt)-string-length($remainder)))+1"/>
        <xsl:choose>
          <xsl:when test="string-length($remainder)=0">
            <xsl:value-of select="string-length($txt)"/>
          </xsl:when>
          <xsl:when test="$lastIndex>0">
            <xsl:value-of select="($lastIndex - string-length($delimiter))"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="0"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>