我們有輸入XML。因此,我們無法維護標籤的順序。 基本上,我們已經在XSLT中編寫了轉換,然後它也損害了2個循環標籤的順序。XSLT:如何維護XML中的標籤的順序
我們想要使用<UserDefined>
標記來維護順序。
輸入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>
我們試圖XSLT就可以了。
<?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>
預期輸出:
<?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>
Csn你發佈你想要的XML輸出,然後你得到並更詳細地解釋你面臨的問題?你有一些匹配'CommentLine'元素的分組代碼,但是然後試圖將'CommentLine'子元素分組,這些元素不是樣本中任何位置的結構。 –
當然這個標籤有號碼。使用這個數字,我們必須按升序排序。我也試圖發佈安排好的輸出XML。 –
NEO
附加的是期望的輸出在編輯question.All我們想看看標籤這是共同的標籤CommentLine和StockLine。 –
NEO