XSLT:如何在单个XML中实现多循环元素

时间:2016-07-18 09:47:12

标签: xml xslt xslt-2.0

我们有输入XML。在那,我们想要多循环两个标签。这些标签是StockLine和CommentLine。我们必须以某种方式将两者结合在一起。当StockLine为null时,则CommentLine,否则当CommentLine为null时为StockLine。

在这种情况下: 我们共有9行商品7 stockLine和2 CommentLine。

输入XML:

<?xml version="1.0" encoding="WINDOWS-1252"?>

-<SalesOrders xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">


-<Orders>


-<OrderHeader>

<CustomerPoNumber>AB-54354</CustomerPoNumber>

<OrderActionType>A</OrderActionType>

<Customer>036938</Customer>

<OrderDate>2016-03-07</OrderDate>

<CustomerName>jennifer straight</CustomerName>

<ShipAddress1>5352 shaw st</ShipAddress1>

<ShipAddress3>NEW PORT RICHEY</ShipAddress3>

<ShipAddress4>FL</ShipAddress4>

<ShipAddress5>US</ShipAddress5>

<OrderDiscPercent1>0.0</OrderDiscPercent1>

<RequestedShipDate>2016-03-07</RequestedShipDate>

<SalesForceOrderNumber>ORD-436326</SalesForceOrderNumber>

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


-<StockLine>

<CustomerPoLine>9999</CustomerPoLine>

<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>

<UserDefined>8</UserDefined>

<OrderLineID>OR-1810124</OrderLineID>

</StockLine>


-<StockLine>

<CustomerPoLine>9999</CustomerPoLine>

<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>

<UserDefined>9</UserDefined>

<OrderLineID>OR-1810125</OrderLineID>

</StockLine>

-

OR-1634834

</OrderDetails>

</Orders>

</SalesOrders>

我们尝试过XSLT:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="Windows-1252" indent="yes"/>

        <xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    <xsl:template match="@*|node()">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="CommentLine[OrderLineID = preceding-sibling::StockLine/OrderLineID and not(Comment)]"/>
    </xsl:stylesheet>

预期产出:

<?xml version="1.0" encoding="WINDOWS-1252"?>

-<SalesOrders xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">


-<Orders>


-<OrderHeader>

<CustomerPoNumber>AB-54354</CustomerPoNumber>

<OrderActionType>A</OrderActionType>

<Customer>036938</Customer>

<OrderDate>2016-03-07</OrderDate>

<CustomerName>jennifer straight</CustomerName>

<ShipAddress1>5352 shaw st</ShipAddress1>

<ShipAddress3>NEW PORT RICHEY</ShipAddress3>

<ShipAddress4>FL</ShipAddress4>

<ShipAddress5>US</ShipAddress5>

<OrderDiscPercent1>0.0</OrderDiscPercent1>

<RequestedShipDate>2016-03-07</RequestedShipDate>

<SalesForceOrderNumber>ORD-436326</SalesForceOrderNumber>

</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>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-1810121</OrderLineID>
             </CommentLine>
<CommentLine>
                <Comment>EDI SAVE</Comment>
                <OrderLineID>OR-1810121</OrderLineID>
             </CommentLine>





</OrderDetails>

</Orders>

</SalesOrders>

1 个答案:

答案 0 :(得分:1)

AFAICT,你想做的只是:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="StockLine[not(StockCode)]">
    <CommentLine>
        <Comment>
            <xsl:value-of select="some-unknown-node"/>
        </Comment>
        <xsl:copy-of select="OrderLineID"/>
    </CommentLine>
</xsl:template>

</xsl:stylesheet>

我无法确定Comment的价值来自哪里。