title: XSLT to combine Line Items with same ID into separate records
body: <p>Trying to combine/ group line items(Header, Line, Charge) having same value for &quot;invoice_id&quot; under one 'Record' dynamically using XSLT.</p>
  <p>In the input XML, there are multiple items such as Header, Line, Charge whose occurrences are unbounded.</p>
  <p>In the output XML, we are trying to group Header, Line and Charges whose invoice id is equal under one parent node &quot;Record&quot;. The parent node &quot;Record&quot; repeats as many times as the invoice id in the input XML.</p>
  <p>Please find the input and expected output below:</p>
  <p><strong>Input XML</strong>
  Input XML is provided below:</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;ns:MT_OkayToPay_Source xmlns:ns=&quot;http://kpmg.com/xi/Coupa/OkayToPay&quot;&gt;
          &lt;Header&gt;
              &lt;type&gt;Header&lt;/type&gt;
              &lt;invoice_id&gt;5&lt;/invoice_id&gt;
          &lt;/Header&gt;
          &lt;Header&gt;
              &lt;type&gt;Header&lt;/type&gt;
              &lt;invoice_id&gt;6&lt;/invoice_id&gt;
          &lt;/Header&gt;
          &lt;Line&gt;
              &lt;type&gt;Line&lt;/type&gt;
              &lt;invoice_id&gt;5&lt;/invoice_id&gt;
          &lt;/Line&gt;
          &lt;Line&gt;
              &lt;type&gt;Line&lt;/type&gt;
              &lt;invoice_id&gt;6&lt;/invoice_id&gt;
          &lt;/Line&gt;
          &lt;Charge&gt;
              &lt;type&gt;Charge&lt;/type&gt;
              &lt;invoice_id&gt;5&lt;/invoice_id&gt;
              &lt;invoice_charge_invoice_id&gt;10&lt;/invoice_charge_invoice_id&gt;
          &lt;/Charge&gt;
          &lt;Charge&gt;
              &lt;type&gt;Charge&lt;/type&gt;
              &lt;invoice_id&gt;5&lt;/invoice_id&gt;
              &lt;invoice_charge_invoice_id&gt;11&lt;/invoice_charge_invoice_id&gt;
          &lt;/Charge&gt;
          &lt;Charge&gt;
              &lt;type&gt;Charge&lt;/type&gt;
              &lt;invoice_id&gt;6&lt;/invoice_id&gt;
              &lt;invoice_charge_invoice_id&gt;13&lt;/invoice_charge_invoice_id&gt;
          &lt;/Charge&gt;
          &lt;Charge&gt;
              &lt;type&gt;Charge&lt;/type&gt;
              &lt;invoice_id&gt;6&lt;/invoice_id&gt;
              &lt;invoice_charge_invoice_id&gt;14&lt;/invoice_charge_invoice_id&gt;
          &lt;/Charge&gt;
  &lt;/ns:MT_OkayToPay_Source&gt;
  </code></pre>
  <p><strong>Output XML</strong>
  The output is expected as below</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;ns:MT_OkayToPay_Source xmlns:ns=&quot;http://kpmg.com/xi/Coupa/OkayToPay&quot;&gt;
      &lt;Record&gt;
      &lt;Header&gt;
          &lt;type&gt;Header&lt;/type&gt;
          &lt;invoice_id&gt;5&lt;/invoice_id&gt;
      &lt;/Header&gt;
      &lt;Line&gt;
          &lt;type&gt;Line&lt;/type&gt;
          &lt;invoice_id&gt;5&lt;/invoice_id&gt;
     &lt;/Line&gt;
      &lt;Charge&gt;
          &lt;type&gt;Charge&lt;/type&gt;
          &lt;invoice_id&gt;5&lt;/invoice_id&gt;
          &lt;invoice_charge_invoice_id&gt;10&lt;/invoice_charge_invoice_id&gt;
      &lt;/Charge&gt;
      &lt;Charge&gt;
          &lt;type&gt;Charge&lt;/type&gt;
          &lt;invoice_id&gt;5&lt;/invoice_id&gt;
          &lt;invoice_charge_invoice_id&gt;11&lt;/invoice_charge_invoice_id&gt;   
      &lt;/Charge&gt;
      &lt;/Record&gt;
      &lt;Record&gt;
      &lt;Header&gt;
          &lt;type&gt;Header&lt;/type&gt;
          &lt;invoice_id&gt;6&lt;/invoice_id&gt;
      &lt;/Header&gt;
      &lt;Line&gt;
          &lt;type&gt;Line&lt;/type&gt;
          &lt;invoice_id&gt;6&lt;/invoice_id&gt;  
      &lt;/Line&gt;
      &lt;Charge&gt;
          &lt;type&gt;Charge&lt;/type&gt;
          &lt;invoice_id&gt;6&lt;/invoice_id&gt;
          &lt;invoice_charge_invoice_id&gt;13&lt;/invoice_charge_invoice_id&gt;   
      &lt;/Charge&gt;
      &lt;Charge&gt;
          &lt;type&gt;Charge&lt;/type&gt;
          &lt;invoice_id&gt;6&lt;/invoice_id&gt;
          &lt;invoice_charge_invoice_id&gt;14&lt;/invoice_charge_invoice_id&gt;   
      &lt;/Charge&gt;
   &lt;/Record&gt;
  &lt;/ns:MT_OkayToPay_Source&gt;
  </code></pre>
  <p>Kindly help.</p>