title: XSLT - for each loop based on sorting
body: <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
      &lt;ns2:CommissionEvents xmlns=&quot;urn:tracelink:mapper:sl:canonical:commontypes&quot; xmlns:ns2=&quot;urn:tracelink:mapper:sl:canonical:serialized_operations_manager&quot;&gt;
          &lt;ns2:CommissionEvent&gt;
             &lt;NumberList&gt;
              &lt;Serial barcodeContent=&quot;(01)00355135132011(21)897883089643(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(01)+AI(21)&quot;&gt;010035513513201121897883089643&lt;/Serial&gt;
              &lt;/NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
          &lt;ns2:CommissionEvent&gt;
             &lt;NumberList&gt;
              &lt;Serial barcodeContent=&quot;(01)00355135132011(21)903131477120(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(01)+AI(21)&quot;&gt;010035513513201121903131477120&lt;/Serial&gt;
             &lt;/NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;ns2:CommissionEvent&gt;
             &lt;NumberList&gt;
              &lt;Serial barcodeContent=&quot;(01)30355135132012(21)389170110454(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;3&quot; format=&quot;AI(01)+AI(21)&quot;&gt;013035513513201221389170110454&lt;/Serial&gt;
             &lt;/NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;ns2:CommissionEvent&gt;
             &lt;NumberList&gt;
              &lt;Serial barcodeContent=&quot;(00)003551350000000907&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(00)&quot;&gt;00003551350000000907&lt;/Serial&gt;
             &lt;/NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;ns2:CommissionEvent&gt;
              &lt;NumberList&gt;
                &lt;Serial barcodeContent=&quot;(00)103551350000000645&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;1&quot; format=&quot;AI(00)&quot;&gt;00103551350000000645&lt;/Serial&gt;
              &lt;/NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;/ns2:CommissionEvents&gt;
  </code></pre>
  <p>The output I'm trying to achieve is something like the below. I tried for each loop grouping based on @filterValue and @format. Could someone help me achieving this using xslt 1.0 or xslt 2.0</p>
  <pre><code>&lt;ns2:CommissionEvents&gt;              
          &lt;ns2:CommissionEvent&gt;
             &lt;ns0:NumberList&gt;
              &lt;ns0:Serial barcodeContent=&quot;(01)00355135132011(21)897883089643(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(01)+AI(21)&quot; /&gt;
              &lt;ns0:Serial barcodeContent=&quot;(01)00355135132011(21)903131477120(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(01)+AI(21)&quot; /&gt;
              &lt;/ns0:NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;ns2:CommissionEvent&gt;
             &lt;ns0:NumberList&gt;
              &lt;ns0:Serial barcodeContent=&quot;(01)30355135132012(21)389170110454(17)230430(10)TLSMKTST&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;3&quot; format=&quot;AI(01)+AI(21)&quot; /&gt;
             &lt;/ns0:NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;ns2:CommissionEvent&gt;
              &lt;ns0:NumberList&gt;
              &lt;ns0:Serial barcodeContent=&quot;(00)003551350000000907&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;0&quot; format=&quot;AI(00)&quot; /&gt;
              &lt;ns0:Serial barcodeContent=&quot;(00)103551350000000645&quot; companyPrefix=&quot;0355135&quot; filterValue=&quot;1&quot; format=&quot;AI(00)&quot; /&gt;
             &lt;/ns0:NumberList&gt;
      &lt;/ns2:CommissionEvent&gt;
      &lt;/ns2:CommissionEvents&gt;
  </code></pre>