<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <!--
    =========================================================================
      Purpose:  Expand the for tag.
    =========================================================================
  -->
  <xsl:template match="for">
    <xsl:call-template name="doIteration">
      <xsl:with-param name="body"       select="*"/>
      <xsl:with-param name="iterations" select="@iterate-over"/>
      <xsl:with-param name="token-name" select="@token"/>
    </xsl:call-template>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Expand a single iteration of the loop.
    =========================================================================
  -->
  <xsl:template name="doIteration">
    <xsl:param    name="body"/>
    <xsl:param    name="iterations" select="string('')"/>
    <xsl:param    name="token-name"/>

    <xsl:variable name="list" select="normalize-space($iterations)"/>
    <xsl:variable name="next" select="normalize-space(substring-before($iterations,','))"/>
    <xsl:variable name="rest" select="normalize-space(substring-after($iterations,','))"/>

    <xsl:if test="not($list = '')">
      <xsl:choose>
        <xsl:when test="contains($list, ',')">
          <xsl:call-template name="expandBody">
            <xsl:with-param name="body"  select="$body"/>
            <xsl:with-param name="token" select="$token-name"/>
            <xsl:with-param name="value" select="$next"/>
          </xsl:call-template>

          <xsl:call-template name="doIteration">
            <xsl:with-param name="body"       select="./*"/>
            <xsl:with-param name="iterations" select="$rest"/>
            <xsl:with-param name="token-name" select="@token"/>
          </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
          <xsl:call-template name="expandBody">
            <xsl:with-param name="body"  select="$body"/>
            <xsl:with-param name="token" select="$token-name"/>
            <xsl:with-param name="value" select="$list"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Expand the loop body for a single loop iteration.
    =========================================================================
  -->
  <xsl:template name="expandBody">
    <xsl:param    name="body"/>
    <xsl:param    name="token"/>
    <xsl:param    name="value"/>

    <xsl:for-each select="$body">
      <!--
      <xsl:message><xsl:text>Do </xsl:text><xsl:value-of select="name(.)"/><xsl:text> with </xsl:text><xsl:value-of select="$token"/><xsl:text> = </xsl:text><xsl:value-of select="$value"/></xsl:message>
      -->

      <xsl:copy>
        <xsl:for-each select="./@*">
          <xsl:attribute name="{name(.)}">
            <xsl:call-template name="replace">
              <xsl:with-param name="text" select="string(.)"/>
              <xsl:with-param name="old"  select="$token"/>
              <xsl:with-param name="new"  select="$value"/>
            </xsl:call-template>
          </xsl:attribute>
        </xsl:for-each>

        <xsl:call-template name="expandBody">
          <xsl:with-param name="body"  select="*"/>
          <xsl:with-param name="token" select="$token"/>
          <xsl:with-param name="value" select="$value"/>
        </xsl:call-template>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Replace a string with another string.
    =========================================================================
  -->
  <xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="old"/>
    <xsl:param name="new"/>

    <xsl:choose>
      <xsl:when test="contains($text, $old)">
        <xsl:value-of select="substring-before($text, $old)"/>
        <xsl:value-of select="$new"/>
        <xsl:call-template name="replace">
          <xsl:with-param name="text" select="substring-after($text,$old)"/>
          <xsl:with-param name="old"  select="$old"/>
          <xsl:with-param name="new"  select="$new"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!--
    =========================================================================
      Purpose:  Copy each node and attribute, exactly as found, to the output
                tree.
    =========================================================================
  -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
