XSLT:How to Maintain Sequential Order of tag in XML

NEO

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.

We wanted to Maintain Sequential Order using <UserDefined> Tag.

Input XML:

  <?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>AB-54354</CustomerPoNumber>
         <OrderActionType>A</OrderActionType>
           </OrderHeader>
      <OrderDetails>
         <CommentLine>
            <Comment>Ensure saddle is color coded</Comment>
            <OrderLineID>OR-1810127</OrderLineID>
            <UserDefined>10</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-001</Comment>
            <OrderLineID>OR-1810128</OrderLineID>
            <UserDefined>11</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-PREVIOUS</Comment>
            <OrderLineID>OR-1810129</OrderLineID>
            <UserDefined>12</UserDefined>
         </CommentLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ACSH-NHH-12OZ-12</StockCode>
            <StockDescription>NHH ABYSS CHIA SHAMPOO 12OZ CS</StockDescription>
            <OrderUom>CS</OrderUom>
            <Price>0.0</Price>
            <PriceUom>CS</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>13</UserDefined>
            <OrderLineID>OR-1810130</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>PAD-DISPLAY-SMH-26</StockCode>
            <StockDescription>PAD TARGET ASSORTMENT 2</StockDescription>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>14</UserDefined>
            <OrderLineID>OR-1810131</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>LADO-SMO-4OZ-01</StockCode>
            <StockDescription>SMO LAVENDER DRY OIL 4OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>7.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>1</UserDefined>
            <OrderLineID>OR-1634834</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>WSSB-SMS-6OZ-01</StockCode>
            <StockDescription>SMW COCONUT SHAVE BUTTER 6OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>7.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>2</UserDefined>
            <OrderLineID>OR-1636755</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHCM-SMH-8OZ-01</StockCode>
            <StockDescription>SMH COCONUT MILK 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>6</UserDefined>
            <OrderLineID>OR-1641836</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHWS-SMO-03</StockCode>
            <StockDescription>SPECIAL2 COCONUT HM/CM/CC</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>25.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>4</UserDefined>
            <OrderLineID>OR-1642378</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHHM-SMH-8OZ-01</StockCode>
            <StockDescription>SMH COCONUT HAIR MIST 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>7</UserDefined>
            <OrderLineID>OR-1642587</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHCC-SMH-8OZ-01</StockCode>
            <StockDescription>SMH CH COWASH CLEANSER 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>5</UserDefined>
            <OrderLineID>OR-1652357</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ABSH-SMH-12OZ-01</StockCode>
            <StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>10.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>3</UserDefined>
            <OrderLineID>OR-1659227</OrderLineID>
         </StockLine>
         <CommentLine>
            <Comment>This is for test purpose</Comment>
            <OrderLineID>OR-1810124</OrderLineID>
            <UserDefined>8</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-SAVE</Comment>
            <OrderLineID>OR-1810125</OrderLineID>
            <UserDefined>9</UserDefined>
         </CommentLine>
      </OrderDetails>
   </Orders>
</SalesOrders>

We tried XSLT On it.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" encoding="Windows-1252" indent="yes" />
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="StockLine[not(StockCodeDescription) and not (OrderQty) and not(Price)]">
      <CommentLine>
         <Comment>
            <xsl:value-of select="StockCode" />
         </Comment>
         <xsl:copy-of select="OrderLineID" />
         <xsl:copy-of select="UserDefined" />
      </CommentLine>
   </xsl:template>
   <xsl:template match="CommentLine">
      <CommentLine>
         <xsl:for-each-group select="CommentLine" group-by="CommentLine">
            <xsl:apply-templates select="current-group()" />
         </xsl:for-each-group>
      </CommentLine>
   </xsl:template>
   <xsl:template match="CommentLine[OrderLineID = preceding-sibling::StockLine/OrderLineID and not(Comment)]" />
   <xsl:template match="CommentLine[some $sib in preceding-sibling::CommentLine satisfies deep-equal(., $sib)]" />
</xsl:stylesheet>

Expected Output :

<?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>AB-54354</CustomerPoNumber>
         <OrderActionType>A</OrderActionType>
      </OrderHeader>
      <OrderDetails>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>LADO-SMO-4OZ-01</StockCode>
            <StockDescription>SMO LAVENDER DRY OIL 4OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>7.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>1</UserDefined>
            <OrderLineID>OR-1634834</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>WSSB-SMS-6OZ-01</StockCode>
            <StockDescription>SMW COCONUT SHAVE BUTTER 6OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>7.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>2</UserDefined>
            <OrderLineID>OR-1636755</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ABSH-SMH-12OZ-01</StockCode>
            <StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>10.99</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>3</UserDefined>
            <OrderLineID>OR-1659227</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHWS-SMO-03</StockCode>
            <StockDescription>SPECIAL2 COCONUT HM/CM/CC</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>25.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>4</UserDefined>
            <OrderLineID>OR-1642378</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHCC-SMH-8OZ-01</StockCode>
            <StockDescription>SMH CH COWASH CLEANSER 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>5</UserDefined>
            <OrderLineID>OR-1652357</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHCM-SMH-8OZ-01</StockCode>
            <StockDescription>SMH COCONUT MILK 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>6</UserDefined>
            <OrderLineID>OR-1641836</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>CHHM-SMH-8OZ-01</StockCode>
            <StockDescription>SMH COCONUT HAIR MIST 8OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>7</UserDefined>
            <OrderLineID>OR-1642587</OrderLineID>
         </StockLine>
         <CommentLine>
            <Comment>This is for test purpose</Comment>
            <OrderLineID>OR-1810124</OrderLineID>
            <UserDefined>8</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-SAVE</Comment>
            <OrderLineID>OR-1810125</OrderLineID>
            <UserDefined>9</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>Ensure saddle is color coded</Comment>
            <OrderLineID>OR-1810127</OrderLineID>
            <UserDefined>10</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-001</Comment>
            <OrderLineID>OR-1810128</OrderLineID>
            <UserDefined>11</UserDefined>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-PREVIOUS</Comment>
            <OrderLineID>OR-1810129</OrderLineID>
            <UserDefined>12</UserDefined>
         </CommentLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ACSH-NHH-12OZ-12</StockCode>
            <StockDescription>NHH ABYSS CHIA SHAMPOO 12OZ CS</StockDescription>
            <OrderUom>CS</OrderUom>
            <Price>0.0</Price>
            <PriceUom>CS</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>13</UserDefined>
            <OrderLineID>OR-1810130</OrderLineID>
         </StockLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>PAD-DISPLAY-SMH-26</StockCode>
            <StockDescription>PAD TARGET ASSORTMENT 2</StockDescription>
            <OrderUom>EA</OrderUom>
            <Price>0.0</Price>
            <PriceUom>EA</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>14</UserDefined>
            <OrderLineID>OR-1810131</OrderLineID>
         </StockLine>
      </OrderDetails>
   </Orders>
</SalesOrders>
Martin Honnen

If all you want is to sort the child elements of the OrderDetails element by the UserDefined element value then

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="OrderDetails">
        <xsl:copy>
            <xsl:apply-templates select="*">
                <xsl:sort select="xs:integer(UserDefined)"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

suffices (Online at http://xsltransform.net/jyRYYjt). I am however not sure why you include all those other templates that seem unrelated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

XSLT transformation for xml with multiple order tag creating corresponding a XML

Split XML with XSLT 1.0 at the location of a particular tag, and maintain HTML markup throughout results

How to read namespaced tag "<a:a>" from XML in XSLT?

How to properly remove an XML tag using XSLT

How sort XML using XSLT ascending order

How to Maintain order of insertion

How to maintain the order of a JSONObject

How to Insert an XML tag after a particular tag using XSLT?

How to maintain order after matching?

Hibernate: how to maintain insertion order

How to maintain list order in Postgres?

How to maintain accordionPanels in the same order?

How to add two xml tag values using a for:each in XSLT?

How to output an IMG tag from XML using XSLT

How to get and convert attributes after tag <product to xml | XSLT

How to dynamically extract a part from XML tag name using XSLT?

Converting tag format in a XML with XSLT

XSLT Change XML elements order

xslt how to remove the parent tag and change attributes of a child tag in an xml doc

HOW TO create a custom xml-tag so that all contents are CDATA-like to be used XML&XSLT?

IntelliJ : How to maintain classpath order like Eclipse

How maintain the order of keys in Java properties file?

How to maintain loading order of script in RequireJS

How to maintain sort order with xargs and gunzip

WSO2 Iterate mediator does not maintain order even though sequential="true"

How to execute Vertx Future in sequential order

How to rename dictionary keys to follow sequential order?

How do I read images in their sequential order?

How to make multiple API calls in sequential order