title: How Do I Duplicate XML based on Child Groups without losing Sibling Data
body: <p>(XSLT 2.0) I am attempting to take a record, and split it into multiple records based off the grouped values of specific child elements - but I need to retain all other elements such that:</p>
  <p><strong>Sample XML</strong></p>
  <pre><code>   &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
      &lt;Report&gt;
          &lt;Record&gt;
              &lt;Name&gt;Testing&lt;/Name&gt;
              &lt;Due_Date&gt;2021-10-08&lt;/Due_Date&gt;
              &lt;Customer&gt;
                  &lt;Name&gt;Test Customer&lt;/Name&gt;
                  &lt;State&gt;GA&lt;/State&gt;
              &lt;/Customer&gt;
              &lt;To_Transaction&gt;
                  &lt;Contact name=&quot;John Smith&quot;&gt;
                      &lt;ID&gt;12345&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;100.00&lt;/Amount&gt;
              &lt;/To_Transaction&gt;
              &lt;To_Transaction&gt;
                  &lt;Contact name=&quot;John Smith&quot;&gt;
                      &lt;ID&gt;12345&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;150.00&lt;/Amount&gt;
              &lt;/To_Transaction&gt;
              &lt;To_Transaction&gt;
                  &lt;Contact name=&quot;Jane Doe&quot;&gt;
                      &lt;ID&gt;54321&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;100.00&lt;/Amount&gt;
              &lt;/To_Transaction&gt;
              &lt;From_Transaction&gt;
                  &lt;Contact name=&quot;John Smith&quot;&gt;
                      &lt;ID&gt;12345&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;8.00&lt;/Amount&gt;
              &lt;/From_Transaction&gt;
              &lt;From_Transaction&gt;
                  &lt;Contact name=&quot;Jane Doe&quot;&gt;
                      &lt;ID&gt;54321&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;75.00&lt;/Amount&gt;
              &lt;/From_Transaction&gt;
              &lt;From_Transaction&gt;
                  &lt;Contact name=&quot;Jane Doe&quot;&gt;
                      &lt;ID&gt;54321&lt;/ID&gt;
                  &lt;/Contact&gt;
                  &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
                  &lt;Amount&gt;50.00&lt;/Amount&gt;
              &lt;/From_Transaction&gt;
          &lt;/Record&gt;
      &lt;/Report&gt;
  </code></pre>
  <p>Becomes... <strong>Expected Result</strong></p>
  <pre><code>&lt;Report&gt;
      &lt;Record&gt;
          &lt;Name&gt;Testing&lt;/Name&gt;
          &lt;Due_Date&gt;2021-10-08&lt;/Due_Date&gt;
          &lt;Customer&gt;
              &lt;Name&gt;Test Customer&lt;/Name&gt;
              &lt;State&gt;GA&lt;/State&gt;
          &lt;/Customer&gt;
          &lt;To_Transaction&gt;
              &lt;Contact name=&quot;John Smith&quot;&gt;
                  &lt;ID&gt;12345&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;100.00&lt;/Amount&gt;
          &lt;/To_Transaction&gt;
          &lt;To_Transaction&gt;
              &lt;Contact name=&quot;John Smith&quot;&gt;
                  &lt;ID&gt;12345&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;150.00&lt;/Amount&gt;
          &lt;/To_Transaction&gt;
          &lt;From_Transaction&gt;
              &lt;Contact name=&quot;John Smith&quot;&gt;
                  &lt;ID&gt;12345&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;john@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;8.00&lt;/Amount&gt;
          &lt;/From_Transaction&gt;
      &lt;/Record&gt;
      &lt;Record&gt;
          &lt;Name&gt;Testing&lt;/Name&gt;
          &lt;Due_Date&gt;2021-10-08&lt;/Due_Date&gt;
          &lt;Customer&gt;
              &lt;Name&gt;Test Customer&lt;/Name&gt;
              &lt;State&gt;GA&lt;/State&gt;
          &lt;/Customer&gt;
          &lt;To_Transaction&gt;
              &lt;Contact name=&quot;Jane Doe&quot;&gt;
                  &lt;ID&gt;54321&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;100.00&lt;/Amount&gt;
          &lt;/To_Transaction&gt;
          &lt;From_Transaction&gt;
              &lt;Contact name=&quot;Jane Doe&quot;&gt;
                  &lt;ID&gt;54321&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;75.00&lt;/Amount&gt;
          &lt;/From_Transaction&gt;
          &lt;From_Transaction&gt;
              &lt;Contact name=&quot;Jane Doe&quot;&gt;
                  &lt;ID&gt;54321&lt;/ID&gt;
              &lt;/Contact&gt;
              &lt;Contact_Email&gt;jane@test.com&lt;/Contact_Email&gt;
              &lt;Amount&gt;50.00&lt;/Amount&gt;
          &lt;/From_Transaction&gt;
      &lt;/Record&gt;
  &lt;/Report&gt;
  </code></pre>
  <p>Ideally, I would be grouping the transaction elements by the Contact @name attribute. The closest I've gotten so far is below. I've tried identity templates but can't figure out how to make it work with the grouping:</p>
  <pre><code>&lt;xsl:stylesheet version=&quot;2.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
      &lt;xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/&gt;
  
      &lt;xsl:template match=&quot;/*&quot;&gt;
          &lt;Report&gt;
              &lt;xsl:for-each-group select=&quot;Record&quot; group-by=&quot;(To_Transaction,'*')&quot;&gt;
                  &lt;Record&gt;
                      &lt;xsl:copy-of select=&quot;.&quot;/&gt;                  
                      &lt;/Record&gt;
                  &lt;/xsl:for-each-group&gt;
          &lt;/Report&gt;
      &lt;/xsl:template&gt;
          
  &lt;/xsl:stylesheet&gt;
  </code></pre>