title: use xsl-apply templates for one-to-many mappings
body: <p>I am trying to do a one-to-many mapping using XSL and I seem to be stuck.</p>
  
  <p>I have a parent tag structure called Channel, that is made of 1-to-many Framework tags. Each framework tag can in turn be made of 1-to-many backend tags. Here is my source XML.</p>
  
  <pre><code>    &lt;ESBConfig&gt;
      &lt;Channels&gt;
          &lt;Channel ChannelId="SMS"&gt;
              &lt;Name&gt;SMS Banking&lt;/Name&gt;
              &lt;dependentFrameworks&gt;
                  &lt;FW FrameworkRef="FW001"&gt;This is to call FW001&lt;/FW&gt;
                  &lt;FW FrameworkRef="FW002"&gt;This is to call FW002&lt;/FW&gt;
              &lt;/dependentFrameworks&gt;
          &lt;/Channel&gt;
          &lt;Channel ChannelId="IBK"&gt;
              &lt;Name&gt;Internet Banking&lt;/Name&gt;
              &lt;dependentFrameworks&gt;
                  &lt;FW FrameworkRef="FW001"&gt;This is to call FW001&lt;/FW&gt;
                  &lt;FW FrameworkRef="FW002"&gt;This is to call FW002&lt;/FW&gt;
              &lt;/dependentFrameworks&gt;
          &lt;/Channel&gt;
      &lt;/Channels&gt;
      &lt;Frameworks&gt;
          &lt;Framework FrameworkId="FW001"&gt;
              &lt;Name&gt;Framework 001&lt;/Name&gt;
              &lt;dependentBackends&gt;
                  &lt;BE BackendRef="BED001"&gt;This is to call BED001&lt;/BE&gt;
                  &lt;BE BackendRef="BED002"&gt;This is to call BED002&lt;/BE&gt;
              &lt;/dependentBackends&gt;
          &lt;/Framework&gt;
          &lt;Framework FrameworkId="FW002"&gt;
              &lt;Name&gt;Framework 002&lt;/Name&gt;
              &lt;dependentBackends&gt;
                  &lt;BE BackendRef="BED001"&gt;This is to call BED001&lt;/BE&gt;
                  &lt;BE BackendRef="BED002"&gt;This is to call BED002&lt;/BE&gt;
              &lt;/dependentBackends&gt;
          &lt;/Framework&gt;
      &lt;/Frameworks&gt;
      &lt;Backends&gt;
          &lt;Backend BackendId="BED001"&gt;
              &lt;Name&gt;Backend 1&lt;/Name&gt;
          &lt;/Backend&gt;
          &lt;Backend BackendId="BED002"&gt;
              &lt;Name&gt;Backend 2&lt;/Name&gt;
          &lt;/Backend&gt;
      &lt;/Backends&gt;
  &lt;/ESBConfig&gt;
  </code></pre>
  
  <p>In my XSL,I am using apply-templates and xsl call-templates. But I am stuck traversing the tree for 1-to-many relationships. This is the target XML i wish to construct. </p>
  
  <pre><code>&lt;ESBConfig&gt;
      &lt;Channel ChannelId="SMS"&gt;
          &lt;Name&gt;SMS Banking&lt;/Name&gt;
          &lt;dependentFrameworks&gt;
              &lt;Framework FrameworkId="FW001"&gt;
                  &lt;Name&gt;Framework 1&lt;/Name&gt;
                  &lt;dependentBackends&gt;
                      &lt;Backend BackendId="BED001"&gt;
                          &lt;Name&gt;Backend 1&lt;/Name&gt;
                      &lt;/Backend&gt;
                      &lt;Backend BackendId="BED002"&gt;
                          &lt;Name&gt;Backend 2&lt;/Name&gt;
                      &lt;/Backend&gt;
                  &lt;/dependentBackends&gt;
              &lt;/Framework&gt;
              &lt;Framework FrameworkId="FW002"&gt;
                  &lt;Name&gt;Framework 2&lt;/Name&gt;
                  &lt;dependentBackends&gt;
                      &lt;Backend BackendId="BED001"&gt;
                          &lt;Name&gt;Backend 1&lt;/Name&gt;
                      &lt;/Backend&gt;
                      &lt;Backend BackendId="BED002"&gt;
                          &lt;Name&gt;Backend 2&lt;/Name&gt;
                      &lt;/Backend&gt;
                  &lt;/dependentBackends&gt;
              &lt;/Framework&gt;
          &lt;/dependentFrameworks&gt;
      &lt;/Channel&gt;
  &lt;/ESBConfig&gt;
  </code></pre>
  
  <p>This is the code I have used so far, but I am unable to iterate over the number of backends for a given framework id. Any pointers are appreciated. Thanks</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:template match="ESBConfig"&gt;
          &lt;xsl:variable name="channelName"&gt;
              &lt;xsl:value-of select="'SMS'"/&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:variable name="frameworkRef"&gt;
              &lt;xsl:value-of select="/ESBConfig/Channels/Channel[@ChannelId=$channelName]/dependentFrameworks/FW/@FrameworkRef"/&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:variable name="backendRef"&gt;
              &lt;xsl:value-of select="/ESBConfig/Frameworks/Framework[@FrameworkId=$frameworkRef]/dependentBackends/BE/@BackendRef"/&gt;
          &lt;/xsl:variable&gt;
          &lt;tr&gt;
              &lt;Level2&gt;
                  &lt;xsl:element name="channelName"&gt;
                      &lt;xsl:value-of select="$channelName"/&gt;
                  &lt;/xsl:element&gt;
                  &lt;channelName&gt;
                      &lt;xsl:value-of select="'SMS'"/&gt;
                  &lt;/channelName&gt;
              &lt;/Level2&gt;
              &lt;Level1&gt;
                  &lt;xsl:element name="frameworkRef"&gt;
                      &lt;xsl:value-of select="$frameworkRef"/&gt;
                  &lt;/xsl:element&gt;
                  &lt;framework&gt;
                      &lt;xsl:copy-of select="/ESBConfig/Frameworks/Framework[@FrameworkId=$frameworkRef]"/&gt;
                  &lt;/framework&gt;
              &lt;/Level1&gt;
              &lt;Level0&gt;
                  &lt;xsl:variable name="snippet"&gt;
                      &lt;xsl:copy-of select="/ESBConfig/Frameworks/Framework[@FrameworkId=$frameworkRef]/dependentBackends/BE"/&gt;
                  &lt;/xsl:variable&gt;
                  &lt;xsl:for-each select="$snippet/BE"&gt;
                      &lt;xsl:variable name="temp"&gt;
                          &lt;xsl:value-of select="@BackendRef"/&gt;
                      &lt;/xsl:variable&gt;
                      &lt;Bref&gt;
                          &lt;xsl:value-of select="$temp"/&gt;
                      &lt;/Bref&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/Level0&gt;
          &lt;/tr&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="/ESBConfig/Backends"&gt;
          &lt;xsl:param name="bid"/&gt;
          &lt;xsl:copy-of select="Backend[@BackendId=$bid]"/&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>