title: Nesting multiple XSLT templates conditionally
body: <p>My XML is very flat, and here is an example:</p>
  
  <pre><code>&lt;Rows&gt;
    &lt;Row&gt;
      &lt;ProjectID&gt;1000&lt;/ProjectID&gt;
      &lt;Phase&gt;Initiation&lt;/Phase&gt;
      &lt;ID&gt;1&lt;/ID&gt;
      &lt;Name&gt;Work item 1&lt;/Name&gt;
      &lt;Master&gt;1&lt;/Master&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;ProjectID&gt;1000&lt;/ProjectID&gt;
      &lt;Phase&gt;Initiation&lt;/Phase&gt;
      &lt;ID&gt;2&lt;/ID&gt;
      &lt;Name&gt;Work item 2&lt;/Name&gt;
      &lt;Master&gt;1&lt;/Master&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;ProjectID&gt;1000&lt;/ProjectID&gt;
      &lt;Phase&gt;Closing&lt;/Phase&gt;
      &lt;ID&gt;3&lt;/ID&gt;
      &lt;Name&gt;Work item 3&lt;/Name&gt;
      &lt;Master&gt;3&lt;/Master&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;ProjectID&gt;1000&lt;/ProjectID&gt;
      &lt;Phase&gt;Closing&lt;/Phase&gt;
      &lt;ID&gt;4&lt;/ID&gt;
      &lt;Name&gt;Work item 4&lt;/Name&gt;
      &lt;Master&gt;3&lt;/Master&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;ProjectID&gt;1000&lt;/ProjectID&gt;
      &lt;Phase&gt;Closing&lt;/Phase&gt;
      &lt;ID&gt;5&lt;/ID&gt;
      &lt;Name&gt;Work item 5&lt;/Name&gt;
      &lt;Master&gt;4&lt;/Master&gt;
    &lt;/Row&gt;
  &lt;/Rows&gt;
  </code></pre>
  
  <p>And they need to be nested in such a way that it displays as such:</p>
  
  <pre><code>**Initiation**
  Work item 1
    Work item 2
  **Closing**
    Work item 3
      Work item 4
        Work item 5
  </code></pre>
  
  <p>Right now I have a template for ProjectID, Phase, and Name (as an example, my actual template are rather large) and I start in the ProjectID template, group and loop by phase and then group and loop by phase-by-name.  (So, I get a list of all names by phase by project).  It worked great for only 2 levels (say work items 1 and 2) but the third level (such as work item 5) have lost me.</p>
  
  <p>Right now I try to iterate over all matching Master fields in the Name template (whose real code is here):</p>
  
  <pre><code>&lt;xsl:template name="Deliverable"&gt;
      &lt;!--
  Parent == True &amp;&amp; Lone == True -&gt; Impossible
  Parent == True &amp;&amp; Lone == False -&gt; Parent
  Parent == False &amp;&amp; Lone == False -&gt; Child
  Parent == False &amp;&amp; Lone = True -&gt; Single deliverable --&gt;
  
      &lt;xsl:param name="parent" /&gt;
      &lt;xsl:param name="lone" /&gt;
  
      &lt;xsl:variable name="Parent"&gt;
        &lt;xsl:choose&gt;
          &lt;xsl:when test="count(key('project-phase-deliverables', concat(ProjectNo, '|', Phase, '|', IDField2))) &gt; 1"&gt;
            1
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
            0
          &lt;/xsl:otherwise&gt;
        &lt;/xsl:choose&gt;
      &lt;/xsl:variable&gt;
  
      &lt;xsl:variable name="ID"&gt;&lt;xsl:value-of select="generate-id(IDField1)" /&gt;&lt;/xsl:variable&gt;
  
      &lt;tr&gt;
        &lt;xsl:choose&gt;
          &lt;!-- JS for parent deliverable --&gt;
          &lt;xsl:when test="$Parent = 'True'"&gt;
            &lt;xsl:attribute name="ID"&gt;
              &lt;xsl:value-of select="$ID"/&gt;
            &lt;/xsl:attribute&gt;
            &lt;script&gt;$('#&lt;xsl:value-of select="$ID"/&gt;').click(function() { $('.&lt;xsl:value-of select="IDField2"/&gt;').toggle(); });&lt;/script&gt;
          &lt;/xsl:when&gt;
          &lt;!-- Coloring/attributes for children --&gt;
          &lt;xsl:when test="$parent = 'False' and $lone='False'"&gt;
            &lt;xsl:attribute name="style"&gt;background: #DEDEFF; display: none;&lt;/xsl:attribute&gt;
            &lt;xsl:attribute name="class"&gt;
              &lt;xsl:value-of select="IDField2"/&gt;
            &lt;/xsl:attribute&gt;
          &lt;/xsl:when&gt;
        &lt;/xsl:choose&gt;
  
        &lt;td class="doWhite" style="width: 15px; height: 24px; text-align:right; border-right:none;"&gt;
          &lt;p class="normal"&gt;
            &lt;!-- Parent deliverable expander arrow --&gt;
            &lt;xsl:if test="$Parent = 1"&gt;
              &lt;span class="Expander"&gt;&amp;#9654;&lt;/span&gt;
            &lt;/xsl:if&gt;
          &lt;/p&gt;
        &lt;/td&gt;
        &lt;td class="doWhite" style="width: 200px; height: 24px; border-left:none;"&gt;
          &lt;p class="normal"&gt;
            &lt;!-- Child deliverable diamond --&gt;
            &lt;xsl:if test="$parent = 'False' and $lone = 'False'"&gt;
              &lt;span class="Expander"&gt;&amp;#9670; &lt;/span&gt;
            &lt;/xsl:if&gt;
            &lt;xsl:value-of select="ItemDescription"/&gt;
          &lt;/p&gt;
        &lt;/td&gt;
        &lt;td class="doWhite" style="width: 130px; height: 24px"&gt;
          &lt;p class="normal"&gt;
            &lt;xsl:value-of select="Owner"/&gt;
          &lt;/p&gt;
        &lt;/td&gt;
        &lt;td style="width: 60px; height: 24px"&gt;
          &lt;xsl:call-template name="status"/&gt;
        &lt;/td&gt;
        &lt;td class="doWhite"&gt;
          &lt;p class="normal"&gt;
            &lt;xsl:value-of select="ItemNotes"/&gt;
          &lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;xsl:if test="$Parent = 1"&gt;
        &lt;xsl:for-each select="key('project-phase-deliverables', concat(ProjectNo, '|', Phase, '|', IDField2))[position()!=1]"&gt;
          &lt;!-- this doesn't recurse properly :( --&gt;
          &lt;xsl:call-template name="Deliverable" /&gt;
        &lt;/xsl:for-each&gt;
      &lt;/xsl:if&gt;
    &lt;/xsl:template&gt;
  </code></pre>
  
  <p>And as you can expect, that loops infinitely and times out.  I feel like I can use apply-template for my problem but how can I use that to effectively group by the other fields (Phase and ProjectID) ?</p>