title: XSLT group by child nodes under each parent
body: <p>I am having some trouble figuring out how to group by child nodes in a for loop like matter. The starting XML looks like this (some nodes are omitted):</p>
  
  <pre><code>&lt;Report&gt;
  &lt;LAC_SalesOrderId&gt;
          &lt;APL&gt;
              &lt;LAC_Customer&gt;09680001&lt;/LAC_Customer&gt;
          &lt;/APL&gt;
          &lt;Detail&gt;
              &lt;LACRecId_DONOT_CHANGE_Textbox169&gt;5637144820&lt;/LACRecId_DONOT_CHANGE_Textbox169&gt;
              &lt;LAC_MarkupTransLines&gt;
                  &lt;MarkupTrans&gt;
                      &lt;Detail_Collection&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Frakt&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                      &lt;/Detail_Collection&gt;
                  &lt;/MarkupTrans&gt;
              &lt;/LAC_MarkupTransLines&gt;
          &lt;/Detail&gt;
      &lt;/LAC_SalesOrderId&gt;
      &lt;LAC_SalesOrderId&gt;
          &lt;APL&gt;
              &lt;LAC_Customer&gt;09680002&lt;/LAC_Customer&gt;
          &lt;/APL&gt;
          &lt;Detail&gt;
              &lt;LACRecId_DONOT_CHANGE_Textbox169&gt;5637144821&lt;/LACRecId_DONOT_CHANGE_Textbox169&gt;
              &lt;LAC_MarkupTransLines&gt;
                  &lt;MarkupTrans&gt;
                      &lt;Detail_Collection&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;20,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Frakt&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;30,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                          &lt;Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;20,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/Detail&gt;
                      &lt;/Detail_Collection&gt;
                  &lt;/MarkupTrans&gt;
              &lt;/LAC_MarkupTransLines&gt;
          &lt;/Detail&gt;
      &lt;/LAC_SalesOrderId&gt;
  &lt;/Report&gt;
  </code></pre>
  
  <p>The desired result would be:</p>
  
  <pre><code>    &lt;Report&gt;
      &lt;LAC_SalesOrderId&gt;
          &lt;APL&gt;
              &lt;LAC_Customer&gt;09680001&lt;/LAC_Customer&gt;
          &lt;/APL&gt;
          &lt;Detail&gt;
              &lt;LACRecId_DONOT_CHANGE_Textbox169&gt;5637144820&lt;/LACRecId_DONOT_CHANGE_Textbox169&gt;
              &lt;LAC_MarkupTransLines&gt;
                  &lt;MarkupTrans&gt;
                      &lt;Detail_Collection&gt;
                          &lt;MarkupCode&gt;
                              &lt;Code&gt;Avgift&lt;/Code&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/MarkupCode&gt;
                          &lt;MarkupCode&gt;
                              &lt;Code&gt;Frakt&lt;/Code&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Frakt&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;10,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/MarkupCode&gt;
                      &lt;/Detail_Collection&gt;
                  &lt;/MarkupTrans&gt;
              &lt;/LAC_MarkupTransLines&gt;
          &lt;/Detail&gt;
      &lt;/LAC_SalesOrderId&gt;
      &lt;LAC_SalesOrderId&gt;
          &lt;APL&gt;
              &lt;LAC_Customer&gt;09680002&lt;/LAC_Customer&gt;
          &lt;/APL&gt;
          &lt;Detail&gt;
              &lt;LACRecId_DONOT_CHANGE_Textbox169&gt;5637144821&lt;/LACRecId_DONOT_CHANGE_Textbox169&gt;
              &lt;LAC_MarkupTransLines&gt;
                  &lt;MarkupTrans&gt;
                      &lt;Detail_Collection&gt;
                          &lt;MarkupCode&gt;
                              &lt;Code&gt;Avgift&lt;/Code&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;20,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Avgift&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;20,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/MarkupCode&gt;
                          &lt;MarkupCode&gt;
                              &lt;Code&gt;Frakt&lt;/Code&gt;
                              &lt;MarkupTrans_Detail&gt;
                                  &lt;FIELD_MarkupTrans_MarkupCode&gt;Frakt&lt;/FIELD_MarkupTrans_MarkupCode&gt;
                                  &lt;FIELD_MarkupTrans_Value&gt;30,00&lt;/FIELD_MarkupTrans_Value&gt;
                              &lt;/MarkupTrans_Detail&gt;
                          &lt;/MarkupCode&gt;
                      &lt;/Detail_Collection&gt;
                  &lt;/MarkupTrans&gt;
              &lt;/LAC_MarkupTransLines&gt;
          &lt;/Detail&gt;
      &lt;/LAC_SalesOrderId&gt;
  &lt;/Report&gt;
  </code></pre>
  
  <p>Up until now I have this XSLT:</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:key name="groups" match="//FIELD_MarkupTrans_MarkupCode" use="."/&gt;
  
  
      &lt;xsl:template match="@* | node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@* | node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
  
      &lt;xsl:template match="//LAC_MarkupTransLines/MarkupTrans/Detail_Collection"&gt;
          &lt;Detail_Collection&gt;
              &lt;xsl:apply-templates select="Detail/MarkupTrans_Detail/FIELD_MarkupTrans_MarkupCode[generate-id() = generate-id(key('groups', .)[1])]"/&gt;
          &lt;/Detail_Collection&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="FIELD_MarkupTrans_MarkupCode"&gt;
          &lt;xsl:variable name="currentGroup" select="."/&gt;
          &lt;MarkupCode&gt;
              &lt;xsl:copy-of select="../FIELD_MarkupTrans_MarkupCode"/&gt;
              &lt;xsl:for-each select="key('groups', $currentGroup)"&gt;
                  &lt;xsl:copy-of select="../.."/&gt;
              &lt;/xsl:for-each&gt;
          &lt;/MarkupCode&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The issue is that all the MarkupTrans_Detail nodes are grouped under the first LAC_SalesOrderId but I need them to be splitted by each LAC_SalesOrderId. I believe that I am missing something obvious and hope that there is someone that can help me figure it out :)</p>