XSLT:如何在XML

时间:2016-07-23 15:01:48

标签: xml templates xslt xslt-2.0

我们有输入XML。因此,我们无法维护标记的连续顺序。 基本上,我们在XSLT中编写了转换。然后它也损害了2个循环标记的顺序。

我们希望使用<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>

1 个答案:

答案 0 :(得分:1)

如果您只想按OrderDetails元素值对UserDefined元素的子元素进行排序,那么

<?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>

足够(http://xsltransform.net/jyRYYjt在线)。但是,我不确定为什么要包含所有其他似乎无关的模板。