我有一個看起來像這樣的XML:使用XSLT組,總和值
<?xml version="1.0" encoding="windows-1252" ?>
<Shipment xmlns="http://oracle.com/EbizGateway/NA/SynchASN/V2">
<containerDetails>
<ContainerID>C123</ContainerID>
<DeliveryContainer>
<ContainerType>Plastic</ContainerType>
<PackedOrder>
<OrderNumber>O1234</OrderNumber>
<PackedItem>
<controldata>
<suppliernumber>A123</suppliernumber>
<customernumner>123</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>3</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
<PackedItem>
<controldata>
<suppliernumber>A123</suppliernumber>
<customernumner>123</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>2</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
<PackedItem>
<controldata>
<suppliernumber>A234</suppliernumber>
<customernumner>234</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>2</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
</PackedOrder>
</DeliveryContainer>
</containerDetails>
</Shipment>
我需要組基礎上的數據筒,suppliernumber,customernumner,ShippedQuantity每個PackedItem。
一旦分組,我需要將所有重複出現的PackedItem的發貨量加起來。對於如:
ContainerID suppliernumber customernumber ShippedQuantity
C123 A123 123 3
C123 A123 123 2
C123 A234 234 2
由於前兩個OCCURENCES是重複的,因此數量需要增加 這必須被轉換爲以下
ContainerID suppliernumber customernumber ShippedQuantity
C123 A123 123 5
C123 A234 234 2
加起來的數量後,我需要更新ShippedQuantity在這兩個重複的事件中,如下面的xml所示。除了ShippedQuantity之外,其餘值將被複制。
<?xml version="1.0" encoding="windows-1252" ?>
<Shipment xmlns="http://oracle.com/EbizGateway/NA/SynchASN/V2">
<containerDetails>
<ContainerID>C123</ContainerID>
<DeliveryContainer>
<ContainerType>Plastic</ContainerType>
<PackedOrder>
<OrderNumber>O1234</OrderNumber>
<PackedItem>
<controldata>
<suppliernumber>A123</suppliernumber>
<customernumner>123</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>5</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
<PackedItem>
<controldata>
<suppliernumber>A123</suppliernumber>
<customernumner>123</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>5</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
<PackedItem>
<controldata>
<suppliernumber>A234</suppliernumber>
<customernumner>234</customernumner>
</controldata>
<ShipmentDetails>
<ShippedQuantity>2</ShippedQuantity>
</ShipmentDetails>
</PackedItem>
</PackedOrder>
</DeliveryContainer>
</containerDetails>
</Shipment>
我試過很多的選擇,但不能真正瞭解如何填充shippedquantities的總和在每個複製packedItem。 :(以下PFB在那裏我嘗試同樣的..它不工作雖然在XSLT ..
<xsl:key name="ContRef" match="tns:PackedItem"
use="concat(../../../tns:ContainerID,../../tns:ContainerType,./tns:controldata/tns:suppliernumber,./tns:controldata/tns:customernumner,./tns:ShipmentDetails/tns:ShippedQuantity,' ',generate-id(./ancestor::tns:containerDetails))"/>
<xsl:template match="/">
<tns:Shipment>
<xsl:for-each select="/tns:Shipment">
<xsl:for-each select="./tns:containerDetails/tns:DeliveryContainer/tns:PackedOrder/tns:PackedItem[generate-id(.)=generate-id(key('ContRef',concat(../../../tns:ContainerID,../../tns:ContainerType,./tns:controldata/tns:suppliernumber,./tns:controldata/tns:customernumner,./tns:ShipmentDetails/tns:ShippedQuantity,' ',generate-id(./ancestor::tns:containerDetails))))]">
<xsl:variable name="ContID"
select="../../../tns:ContainerID"/>
<xsl:variable name="CustPartNumber"
select="./tns:controldata/tns:customernumner"/>
<xsl:variable name="SuppPartNumber"
select="./tns:controldata/tns:suppliernumber"/>
<xsl:variable name="ShippedQty"
select="./tns:ShipmentDetails/tns:ShippedQuantity"/>
<!-- calculate total shipped quantity-->
<xsl:variable name="TotalShippedQuantity"
select="sum(key('ContRef',concat(../../../tns:ContainerID,../../tns:ContainerType,./tns:controldata/tns:suppliernumber,./tns:controldata/tns:customernumner,./tns:ShipmentDetails/tns:ShippedQuantity,' ',generate-id(./ancestor::tns:containerDetails)))/tns:ShipmentDetails/tns:ShippedQuantity)"/>
<!-- populate the total shipped quantity in each duplicate packed item -->
<xsl:for-each select="../tns:PackedItem[./tns:controldata/tns:customernumner=$CustPartNumber and ./tns:controldata/tns:suppliernumber=$SuppPartNumber and ./tns:ShipmentDetails/tns:ShippedQuantity=$ShippedQty]/tns:ShipmentDetails/tns:ShippedQuantity">
<xsl:text>inside for</xsl:text>
<xsl:value-of select="$TotalShippedQuantity"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</tns:Shipment>
</xsl:template>
請指點。
HTTP:/在我的問題上面的PFA xslt(我試過)/ /sackoverflow.com/questions/28259342/how-to-group-data-from-one-xml-to-other-using-xslt#comment44879293_28259342 – 2015-02-05 17:41:04
PFA xslt(我試過)。我會非常感謝這個提示。 – moon 2015-02-06 07:34:24
我現在可以在我的xslt中通過以下修改來計算總和。但是,仍然不知道如何將此值填充到重複的PackedItems中,並按照原樣複製剩餘的有效內容。 –
moon
2015-02-06 08:27:57