title: XSLT:How to Maintain Sequential Order of tag in XML
body: <p>We have Input XML.In that,We are not able to maintain sequential order of tag.
  Basically,We have written transformation in XSLT.Then it also harms sequential order of 2 Looping tag. </p>
  
  <p>We wanted to Maintain Sequential Order using <code>&lt;UserDefined&gt;</code> Tag.</p>
  
  <p><strong>Input XML:</strong></p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD"&gt;
     &lt;Orders&gt;
        &lt;OrderHeader&gt;
           &lt;CustomerPoNumber&gt;AB-54354&lt;/CustomerPoNumber&gt;
           &lt;OrderActionType&gt;A&lt;/OrderActionType&gt;
             &lt;/OrderHeader&gt;
        &lt;OrderDetails&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;Ensure saddle is color coded&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810127&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;10&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-001&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810128&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;11&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-PREVIOUS&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810129&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;12&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;ACSH-NHH-12OZ-12&lt;/StockCode&gt;
              &lt;StockDescription&gt;NHH ABYSS CHIA SHAMPOO 12OZ CS&lt;/StockDescription&gt;
              &lt;OrderUom&gt;CS&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;CS&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;13&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1810130&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;PAD-DISPLAY-SMH-26&lt;/StockCode&gt;
              &lt;StockDescription&gt;PAD TARGET ASSORTMENT 2&lt;/StockDescription&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;14&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1810131&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;LADO-SMO-4OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMO LAVENDER DRY OIL 4OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;7.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;1&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1634834&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;WSSB-SMS-6OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMW COCONUT SHAVE BUTTER 6OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;7.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;2&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1636755&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHCM-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH COCONUT MILK 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;6&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1641836&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHWS-SMO-03&lt;/StockCode&gt;
              &lt;StockDescription&gt;SPECIAL2 COCONUT HM/CM/CC&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;25.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;4&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1642378&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHHM-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH COCONUT HAIR MIST 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;7&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1642587&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHCC-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH CH COWASH CLEANSER 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;5&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1652357&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;ABSH-SMH-12OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH ABS BALANCE SHAMPOO 12OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;10.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;3&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1659227&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;This is for test purpose&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810124&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;8&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-SAVE&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810125&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;9&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
        &lt;/OrderDetails&gt;
     &lt;/Orders&gt;
  &lt;/SalesOrders&gt;
  </code></pre>
  
  <p><strong>We tried XSLT On it.</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
     &lt;xsl:output method="xml" encoding="Windows-1252" indent="yes" /&gt;
     &lt;xsl:template match="@* | node()"&gt;
        &lt;xsl:copy&gt;
           &lt;xsl:apply-templates select="@* | node()" /&gt;
        &lt;/xsl:copy&gt;
     &lt;/xsl:template&gt;
     &lt;xsl:template match="StockLine[not(StockCodeDescription) and not (OrderQty) and not(Price)]"&gt;
        &lt;CommentLine&gt;
           &lt;Comment&gt;
              &lt;xsl:value-of select="StockCode" /&gt;
           &lt;/Comment&gt;
           &lt;xsl:copy-of select="OrderLineID" /&gt;
           &lt;xsl:copy-of select="UserDefined" /&gt;
        &lt;/CommentLine&gt;
     &lt;/xsl:template&gt;
     &lt;xsl:template match="CommentLine"&gt;
        &lt;CommentLine&gt;
           &lt;xsl:for-each-group select="CommentLine" group-by="CommentLine"&gt;
              &lt;xsl:apply-templates select="current-group()" /&gt;
           &lt;/xsl:for-each-group&gt;
        &lt;/CommentLine&gt;
     &lt;/xsl:template&gt;
     &lt;xsl:template match="CommentLine[OrderLineID = preceding-sibling::StockLine/OrderLineID and not(Comment)]" /&gt;
     &lt;xsl:template match="CommentLine[some $sib in preceding-sibling::CommentLine satisfies deep-equal(., $sib)]" /&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Expected Output :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD"&gt;
     &lt;Orders&gt;
        &lt;OrderHeader&gt;
           &lt;CustomerPoNumber&gt;AB-54354&lt;/CustomerPoNumber&gt;
           &lt;OrderActionType&gt;A&lt;/OrderActionType&gt;
        &lt;/OrderHeader&gt;
        &lt;OrderDetails&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;LADO-SMO-4OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMO LAVENDER DRY OIL 4OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;7.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;1&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1634834&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;WSSB-SMS-6OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMW COCONUT SHAVE BUTTER 6OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;7.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;2&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1636755&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;ABSH-SMH-12OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH ABS BALANCE SHAMPOO 12OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;10.99&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;3&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1659227&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHWS-SMO-03&lt;/StockCode&gt;
              &lt;StockDescription&gt;SPECIAL2 COCONUT HM/CM/CC&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;25.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;4&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1642378&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHCC-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH CH COWASH CLEANSER 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;5&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1652357&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHCM-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH COCONUT MILK 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;6&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1641836&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;CHHM-SMH-8OZ-01&lt;/StockCode&gt;
              &lt;StockDescription&gt;SMH COCONUT HAIR MIST 8OZ&lt;/StockDescription&gt;
              &lt;OrderQty&gt;1.0&lt;/OrderQty&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;7&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1642587&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;This is for test purpose&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810124&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;8&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-SAVE&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810125&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;9&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;Ensure saddle is color coded&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810127&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;10&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-001&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810128&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;11&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;CommentLine&gt;
              &lt;Comment&gt;EDI-PREVIOUS&lt;/Comment&gt;
              &lt;OrderLineID&gt;OR-1810129&lt;/OrderLineID&gt;
              &lt;UserDefined&gt;12&lt;/UserDefined&gt;
           &lt;/CommentLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;ACSH-NHH-12OZ-12&lt;/StockCode&gt;
              &lt;StockDescription&gt;NHH ABYSS CHIA SHAMPOO 12OZ CS&lt;/StockDescription&gt;
              &lt;OrderUom&gt;CS&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;CS&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;13&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1810130&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
           &lt;StockLine&gt;
              &lt;CustomerPoLine&gt;9999&lt;/CustomerPoLine&gt;
              &lt;StockCode&gt;PAD-DISPLAY-SMH-26&lt;/StockCode&gt;
              &lt;StockDescription&gt;PAD TARGET ASSORTMENT 2&lt;/StockDescription&gt;
              &lt;OrderUom&gt;EA&lt;/OrderUom&gt;
              &lt;Price&gt;0.0&lt;/Price&gt;
              &lt;PriceUom&gt;EA&lt;/PriceUom&gt;
              &lt;AlwaysUsePriceEntered&gt;Y&lt;/AlwaysUsePriceEntered&gt;
              &lt;UserDefined&gt;14&lt;/UserDefined&gt;
              &lt;OrderLineID&gt;OR-1810131&lt;/OrderLineID&gt;
           &lt;/StockLine&gt;
        &lt;/OrderDetails&gt;
     &lt;/Orders&gt;
  &lt;/SalesOrders&gt;
  </code></pre>