<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"  version = "1.1" >
<xsl:preserve-space elements="*"/>
<xsl:output method="xml" indent="no"/> 

<xsl:template match="*" >
 <xsl:copy>
  <xsl:apply-templates select="@*|*|text()" />
 </xsl:copy><xsl:value-of select="$newline"/>
</xsl:template>

<xsl:template match="@*|text()" >
 <xsl:copy>
  <xsl:apply-templates select="@*|*|text()" />
 </xsl:copy>
</xsl:template>

<xsl:template match="camp">
<camp>
 <xsl:call-template name="removedo">
  <xsl:with-param name="cmnds" select="cmnd"/>
 </xsl:call-template>
</camp>
</xsl:template>


<xsl:template name="removedo">
<xsl:param name="cmnds"/>
<xsl:choose>
 <xsl:when test="count($cmnds) = 0"></xsl:when>
 <xsl:otherwise>
 <xsl:choose>
  <xsl:when test="$cmnds[1]/@name !='DO'">
   <xsl:copy-of select="$cmnds[1]"/>
   <xsl:call-template name="removedo">
    <xsl:with-param name="cmnds" select="$cmnds[position() != 1]"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
  <xsl:variable name="ntimes" select="$cmnds[1]/@NTIMES"/>
   <xsl:variable name="inloop">
   <allcmnds>
    <xsl:call-template name="collectloop">
     <xsl:with-param name="restofcommands" select="$cmnds[position() != 1]"/>
    </xsl:call-template>
    </allcmnds>
   </xsl:variable>

   <cmnd name="MULTV" VAR="9" OPRND="0." BGIN="0" END="0" C="CLEARVAR9"/>

   <xsl:call-template name="loopcopies">
   <xsl:with-param name="inloop" select="$inloop"/>
   <xsl:with-param name="ntimes" select="$ntimes" />
   </xsl:call-template>
   <xsl:copy-of select="$inloop/allcmnds/remaining/*"/>
  </xsl:otherwise>
 </xsl:choose>

</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="collectloop">
<xsl:param name="restofcommands"/>
 <xsl:choose>
  <xsl:when test="$restofcommands[1]/@name !='LOOP'">
   <xsl:copy-of select="$restofcommands[1]"/>
   <xsl:call-template name="collectloop">
    <xsl:with-param name="restofcommands" select="$restofcommands[position() != 1]"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
   <remaining><xsl:copy-of select="$restofcommands[position() != 1]"/></remaining>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:template name="loopcopies">
 <xsl:param name="inloop"/>
 <xsl:param name="ntimes"/>
 <xsl:choose>
  <xsl:when test="$ntimes != 0">
  <cmnd name="ADDV" VAR="9" OPRND="1." BGIN="0" END="0" C="ADD 1 TO VAR 9"/>

   <xsl:copy-of select="$inloop/allcmnds/cmnd"/>
   <xsl:call-template name="loopcopies">
     <xsl:with-param name="inloop" select="$inloop"/>
     <xsl:with-param name="ntimes" select="$ntimes - 1" />
   </xsl:call-template>
  </xsl:when>
  <xsl:otherwise/>
 </xsl:choose>
</xsl:template>


</xsl:stylesheet>