title: Grouping nodes by value of grandchildren
body: <p>I have an XML that has a group type as the deepest node. I have to group a subsection of the XML, but otherwise return the exact same XML. </p>
  
  <p>I have looked at different questions in Stackoverflow. Specifically, I have been basing my answer on this one:
  <a href="https://stackoverflow.com/questions/4711039/grouping-xml-nodes-by-value-of-a-child-in-xsl">Grouping xml nodes by value of a child in Xsl</a></p>
  
  <p>The input XML looks like this:</p>
  
  <pre><code>&lt;list&gt;
      &lt;element&gt;
          &lt;foo&gt;
              &lt;bar&gt;
              &lt;/bar&gt;
          &lt;/foo&gt;
          &lt;values&gt;
              &lt;position&gt;
                  &lt;foo2&gt;
                  &lt;/foo2&gt;
                  &lt;type&gt;first
                  &lt;/type&gt;
              &lt;/position&gt;
              &lt;position&gt;
                  &lt;foo2&gt;
                  &lt;/foo2&gt;
                  &lt;type&gt;second
                  &lt;/type&gt;
              &lt;/position&gt;
              &lt;position&gt;
                  &lt;foo2&gt;
                  &lt;/foo2&gt;
                  &lt;type&gt;first
                  &lt;/type&gt;
              &lt;/position&gt;
          &lt;/values&gt;
      &lt;/element&gt;    
  &lt;/list&gt;
  </code></pre>
  
  <p>The output XML should look like this:</p>
  
  <pre><code>&lt;list&gt;
      &lt;element&gt;
          &lt;foo&gt;
              &lt;bar&gt;
              &lt;/bar&gt;
          &lt;/foo&gt;
          &lt;types&gt;
              &lt;first&gt;
                  &lt;values&gt;
                      &lt;position&gt;
                          &lt;foo2&gt;
                          &lt;/foo2&gt;
                      &lt;/position&gt;
                      &lt;position&gt;
                          &lt;foo2&gt;
                          &lt;/foo2&gt;
                      &lt;/position&gt;
                  &lt;/values&gt;
              &lt;/first&gt;
              &lt;second&gt;
                  &lt;values&gt;
                      &lt;position&gt;
                          &lt;foo2&gt;
                          &lt;/foo2&gt;
                      &lt;/position&gt;
                  &lt;/values&gt;
              &lt;/second&gt;
          &lt;/types&gt;
      &lt;/element&gt;    
  &lt;/list&gt;
  </code></pre>
  
  <p>Up until now I have the following XLST which copies the transform on the top:
      
      </p>
  
  <pre><code>    &lt;xsl:key name="types" match="type" use="."/&gt;
  
      &lt;xsl:template match="/list"&gt;
          &lt;root&gt;
              &lt;xsl:apply-templates select="element/values/position/type[generate-id() = generate-id(key('types', .)[1])]"/&gt;
              &lt;xsl:apply-templates select="*|@*|comment()|text()"/&gt;
          &lt;/root&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="type"&gt;
          &lt;xsl:variable name="currentType" select="."/&gt;
          &lt;xsl:element name="{$currentType}"&gt;
              &lt;xsl:apply-templates select="*|@*|comment()|text()"/&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="*"&gt;
          &lt;xsl:element name="{local-name(.)}"&gt;
              &lt;xsl:apply-templates select="*|@*|comment()|text()"/&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>As soon as I put an apply templates on the parent nodes (in the template that matches type), it does not return anything anymore:</p>
  
  <pre><code>    &lt;xsl:template match="type"&gt;
          &lt;xsl:variable name="currentType" select="."/&gt;
          &lt;xsl:element name="{$currentType}"&gt;
              &lt;xsl:apply-templates select="../../*|@*|comment()|text()"/&gt;
          &lt;/xsl:element&gt;
  </code></pre>