title: Grouping Child Elements within Grouped Parent Elements XSLT
body: <p>I am working on combining XML elements using <code>for-each-group</code> and the problem I am having is that these XML elements have children elements that have children of their own.
  I would also like to combine the children elements as well.</p>
  
  <p>My input XML looks like this:</p>
  
  <pre><code>&lt;Root&gt;
     &lt;Header&gt;
     &lt;/Header&gt;
     &lt;Summary ID="1"&gt;
        &lt;ID&gt;1&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;340.00&lt;/Net_Sales_Amount&gt;
        &lt;DATA_From_2&gt;
           &lt;Sales_Amount&gt;340.00&lt;/_Sales_Amount&gt;
        &lt;/DATA_From_2&gt;
     &lt;/Summary&gt;
     &lt;Summary ID="1"&gt;
        &lt;ID&gt;1&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Batch_Number&gt;04002&lt;/Batch_Number&gt;
        &lt;Net_Sales_Amount&gt;130.00&lt;/Net_Sales_Amount&gt;
        &lt;DATA_From_2&gt;
           &lt;Sales_Amount&gt;130.00&lt;/Sales_Amount&gt;
        &lt;/DATA_From_2&gt;
        &lt;DATA_From_8&gt;
           &lt;Invoice_Amount&gt;20&lt;/Invoice_Amount&gt;
           &lt;Invoice_Number&gt;123&lt;/Invoice_Number&gt;
        &lt;/DATA_From_8&gt;
        &lt;DATA_From_8&gt;
           &lt;Invoice_Amount&gt;15&lt;/Invoice_Amount&gt;
           &lt;Invoice_Number&gt;456&lt;/Invoice_Number&gt;
        &lt;/DATA_From_8&gt;
     &lt;/Summary&gt;
     &lt;Summary ID="2"&gt;
        &lt;ID&gt;2&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;40.00&lt;/Net_Sales_Amount&gt;
        &lt;DATA_From_2&gt;
           &lt;Sales_Amount&gt;40.00&lt;/Sales_Amount&gt;
        &lt;/DATA_From_2&gt;
     &lt;/Summary&gt;
     &lt;Summary ID="2"&gt;
        &lt;ID&gt;2&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;819.91&lt;/Net_Sales_Amount&gt;
        &lt;DATA_From_2&gt;
           &lt;Sales_Amount&gt;734.91&lt;/Sales_Amount&gt;
        &lt;/DATA_From_2&gt;
        &lt;DATA_From_5&gt;
           &lt;Sales_Amount&gt;85.00&lt;/Sales_Amount&gt;
        &lt;/DATA_From_5&gt;
     &lt;/Summary&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Here is my XSLT file. I get close to my desired output, but instead of combining the same children elements, it puts all of them inside the <code>Summary</code> element.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output indent="yes" /&gt;
  
      &lt;xsl:template match="/*"&gt;
          &lt;xsl:copy&gt;
              &lt;Root&gt;
                  &lt;xsl:for-each-group select="/Root/Summary" group-by="@ID"&gt;
                      &lt;Summary ID="{@ID}"&gt;
                          &lt;!-- Copy values that are the same --&gt;
                          &lt;xsl:sequence select="ID, Batch_Date"/&gt;
  
                          &lt;!-- Sum the amounts --&gt;
                          &lt;Net_Sales_Amount&gt;
                              &lt;xsl:value-of select="format-number(sum(current-group()/Net_Sales_Amount), '0.00')"/&gt;
                          &lt;/Net_Sales_Amount&gt;
  
                          &lt;!-- Combine the record types in this Summary element --&gt;
  &lt;!--                        &lt;xsl:sequence select="DATA_From_2, DATA_From_3, DATA_From_5, DATA_From_8"/&gt; --&gt;
  
                          &lt;xsl:for-each-group select="current-group()" group-by="DATA_From_2"&gt;
                              &lt;Data_From_2&gt;
                                  &lt;Sales_Amount&gt;
                                      &lt;xsl:value-of select="format-number(sum(current-group()/Data_From_2/Combined_Sales_Amount), '0.00')"/&gt;
                                  &lt;/Sales_Amount&gt;
                              &lt;/Data_From_2&gt;
                          &lt;/xsl:for-each-group&gt;
  
                          &lt;xsl:for-each-group select="current-group()" group-by="DATA_From_8"&gt;
                              &lt;Data_From_8&gt;
                                  &lt;Invoice_Amount&gt;
                                      &lt;xsl:value-of select="format-number(sum(current-group()/Data_From_8/Invoice_Amount), '0.00')"/&gt;
                                  &lt;/Invoice_Amount&gt;
                                  &lt;Invoice_Number&gt;
                                      &lt;xsl:value-of select="current-group()/Data_From_8/Invoice_Number" separator=" | "/&gt;
                                  &lt;/Invoice_Number&gt;
                              &lt;/Data_From_8&gt;
                          &lt;/xsl:for-each-group&gt;
                      &lt;/Summary&gt;
                  &lt;/xsl:for-each-group&gt;
  
                  &lt;Header&gt;
                  &lt;/Header&gt;
                  &lt;Footer&gt;
                  &lt;/Footer&gt;
              &lt;/Root&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Here is my current output:</p>
  
  <pre><code>&lt;Root&gt;
     &lt;Summary ID="1"&gt;
        &lt;ID&gt;1&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;470.00&lt;/Net_Sales_Amount&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;0.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;0.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;Data_From_8&gt;
           &lt;Invoice_Amount&gt;0.00&lt;/Invoice_Amount&gt;
           &lt;Invoice_Number/&gt;
        &lt;/Data_From_8&gt;
        &lt;Data_From_8&gt;
           &lt;Invoice_Amount&gt;0.00&lt;/Invoice_Amount&gt;
           &lt;Invoice_Number/&gt;
        &lt;/Data_From_8&gt;
     &lt;/Summary&gt;
     &lt;Summary ID="2"&gt;
        &lt;ID&gt;2&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;859.91&lt;/Net_Sales_Amount&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;0.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;0.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;0.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
     &lt;/Summary&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The XSLT is not combining the Data_From elements that are the same, and it's also not populating the values correctly.</p>
  
  <p>This is what it should look like:</p>
  
  <pre><code>&lt;Root&gt;
     &lt;Summary ID="1"&gt;
        &lt;ID&gt;1&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;470.00&lt;/Net_Sales_Amount&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;470.00&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;Data_From_8&gt;
           &lt;Invoice_Amount&gt;35.00&lt;/Invoice_Amount&gt;
           &lt;Invoice_Number/&gt;
        &lt;/Data_From_8&gt;
     &lt;/Summary&gt;
     &lt;Summary ID="2"&gt;
        &lt;ID&gt;2&lt;/ID&gt;
        &lt;Batch_Date&gt;0228&lt;/Batch_Date&gt;
        &lt;Net_Sales_Amount&gt;859.91&lt;/Net_Sales_Amount&gt;
        &lt;Data_From_2&gt;
           &lt;Sales_Amount&gt;771.91&lt;/Sales_Amount&gt;
        &lt;/Data_From_2&gt;
        &lt;DATA_From_5&gt;
           &lt;Sales_Amount&gt;85.00&lt;/Sales_Amount&gt;
        &lt;/DATA_From_5&gt;
     &lt;/Summary&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The XSLT should combine the similar elements (Data_from_2, Data_from_5, etc) and add up the values.
  I apologize for any typos or incorrect XML, I did a lot of copy and paste.
  Thanks</p>