title: Group XML based on key
body: <p>'Header(Max 1)
  Details(Multiple)
  PackingList(Max 1 &amp; Optional)</p>
  
  <p>InvoiceNumber is the key for Header, Details &amp; Packing List.'
  'My Input'</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;ns0:SupplierInvoiceProcessing xmlns:ns0="test"&gt;
      &lt;Invoice&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;1&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;Test4&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/Invoice&gt;
      &lt;Invoice&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;2&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;c3&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;a1&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/Invoice&gt;
      &lt;Invoice&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;1&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;/Invoice&gt;
      &lt;Invoice&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;2&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;5&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;d5&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;6&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;e6&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;b2&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/Invoice&gt;
      &lt;/ns0:SupplierInvoiceProcessing&gt;
  </code></pre>
  
  <p>'I changed the xslt code to but still needs code modification to'</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:ns0="test" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:template match="/*"&gt;
  &lt;SupplierAdvanceShipmentNotification&gt;
  &lt;xsl:for-each-group select="Invoice[Header]/(Header, Details, PackingList)" group-by="InvoiceNumber"&gt;
  &lt;AdvanceShipmentNotifications&gt;
  &lt;ASN&gt;
  &lt;xsl:copy-of select="current-group()[self::Header], current-group()[self::Details], current-group()[self::PackingList]"/&gt;
  &lt;/ASN&gt;
  &lt;/AdvanceShipmentNotifications&gt;
  &lt;/xsl:for-each-group&gt;
  &lt;/SupplierAdvanceShipmentNotification&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>'I expect output to not include the ASN without Header.
  Header, Details, Packing List grouped in ASN is mandatory. Need to strip any extra or no matching Packing Lists with Header.'</p>
  
  <pre><code>    &lt;SupplierAdvanceShipmentNotificationxmlns:ns0="test"&gt;
      &lt;AdvanceShipmentNotifications&gt;
      &lt;ASN&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;1&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;a4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;1&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;a1&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/ASN&gt;
      &lt;ASN&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;2&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;d4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;4&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;Test4&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/ASN&gt;
      &lt;ASN&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;2&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;b4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;2&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;b2&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/ASN&gt;
      &lt;ASN&gt;
      &lt;Header&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;ETA&gt;1&lt;/ETA&gt;
      &lt;/Header&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c1&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c2&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c3&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;Details&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;OrderNumber&gt;c4&lt;/OrderNumber&gt;
      &lt;/Details&gt;
      &lt;PackingList&gt;
      &lt;ETA&gt;20190221&lt;/ETA&gt;
      &lt;InvoiceNumber&gt;3&lt;/InvoiceNumber&gt;
      &lt;FOBTotalNoOfCartons&gt;c3&lt;/FOBTotalNoOfCartons&gt;
      &lt;/PackingList&gt;
      &lt;/ASN&gt;
      &lt;/AdvanceShipmentNotifications&gt;
      &lt;/SupplierAdvanceShipmentNotification&gt;
  </code></pre>