我的场景是xml-idoc,其中idoc我有E1EDP01-POSEX,它会重复值000001.我找到了如何删除重复节点但在此之前我需要在E1EDP01-MENGE中添加值(ex posex = 000001)和menge = 2500,posex = 000001和menge3000)所以我想要posex = 000001和menge = 5500。
我使用此代码
<xsl:template match="E1EDP01[not(generate-id() = generate-id(key('kPOSEX', POSEX)[000001]))]"/>
它只占用第一个节点值。以下是相同的代码
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Invoice>
<InvoiceDetail>
<ListOfInvoiceItemDetail>
<InvoiceItemDetail>
<InvoiceBaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>000002</BuyerLineItemNum>
<SellerLineItemNum>000001</SellerLineItemNum>
</LineItemNum>
<LineItemType>
<LineItemTypeCoded>Item</LineItemTypeCoded>
</LineItemType>
<ItemIdentifiers>
<PartNumbers>
<SellerPartNumber>
<PartNum>
<PartID>100356381</PartID>
</PartNum>
</SellerPartNumber>
<BuyerPartNumber>
<PartNum>
<PartID>517801</PartID>
</PartNum>
</BuyerPartNumber>
</PartNumbers>
</ItemIdentifiers>
<TotalQuantity>
<Quantity>
<QuantityValue>40320.0</QuantityValue>
<UnitOfMeasurement>
<UOMCoded>EA</UOMCoded>
</UnitOfMeasurement>
</Quantity>
</TotalQuantity>
<LineItemReferences>
<InvoiceReferences>
<PurchaseOrderReference>
<PurchaseOrderNumber>
<Reference>
<RefNum>0045486186</RefNum>
</Reference>
</PurchaseOrderNumber>
<PurchaseOrderDate>20110825T040000.000Z</PurchaseOrderDate>
<PurchaseOrderLineItemNumber>000002</PurchaseOrderLineItemNumber>
</PurchaseOrderReference>
<ASNNumber>
<Reference>
<RefNum>0805548867</RefNum>
</Reference>
</ASNNumber>
<OtherInvoiceReferences>
<ListOfReferenceCoded>
<ReferenceCoded>
<ReferenceTypeCoded>Other</ReferenceTypeCoded>
<ReferenceTypeCodedOther>SalesOrderNumber</ReferenceTypeCodedOther>
<PrimaryReference>
<Reference>
<RefNum>0006543680</RefNum>
</Reference>
</PrimaryReference>
<SupportingReference>
<Reference>
<RefNum>000002</RefNum>
</Reference>
</SupportingReference>
</ReferenceCoded>
</ListOfReferenceCoded>
</OtherInvoiceReferences>
</InvoiceReferences>
</LineItemReferences>
</InvoiceBaseItemDetail>
<InvoicePricingDetail>
<ListOfPrice>
<Price>
<UnitPrice>
<UnitPriceValue>115.11</UnitPriceValue>
</UnitPrice>
</Price>
</ListOfPrice>
<Tax>
<TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded>
<TaxCategoryCoded>StandardRate</TaxCategoryCoded>
<TaxTypeCoded>Other</TaxTypeCoded>
<TaxTypeCodedOther>
<Identifier>
<Agency>
<AgencyCoded>Other</AgencyCoded>
<AgencyCodedOther>Not Specified</AgencyCodedOther>
</Agency>
<Ident>National Tax</Ident>
</Identifier>
</TaxTypeCodedOther>
<TaxPercent>10.000</TaxPercent>
<TaxAmount>464.12</TaxAmount>
</Tax>
<InvoiceCurrencyTotalValue>
<MonetaryValue>
<MonetaryAmount>4641.24</MonetaryAmount>
</MonetaryValue>
</InvoiceCurrencyTotalValue>
</InvoicePricingDetail>
</InvoiceItemDetail>
<InvoiceItemDetail>
<InvoiceBaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>000001</BuyerLineItemNum>
<SellerLineItemNum>000002</SellerLineItemNum>
</LineItemNum>
<LineItemType>
<LineItemTypeCoded>Item</LineItemTypeCoded>
</LineItemType>
<ItemIdentifiers>
<PartNumbers>
<SellerPartNumber>
<PartNum>
<PartID>100351756</PartID>
</PartNum>
</SellerPartNumber>
<BuyerPartNumber>
<PartNum>
<PartID>462088</PartID>
</PartNum>
</BuyerPartNumber>
</PartNumbers>
</ItemIdentifiers>
<TotalQuantity>
<Quantity>
<QuantityValue>37632.0</QuantityValue>
<UnitOfMeasurement>
<UOMCoded>EA</UOMCoded>
</UnitOfMeasurement>
</Quantity>
</TotalQuantity>
<LineItemReferences>
<InvoiceReferences>
<PurchaseOrderReference>
<PurchaseOrderNumber>
<Reference>
<RefNum>0045486186</RefNum>
</Reference>
</PurchaseOrderNumber>
<PurchaseOrderDate>20110825T040000.000Z</PurchaseOrderDate>
<PurchaseOrderLineItemNumber>000001</PurchaseOrderLineItemNumber>
</PurchaseOrderReference>
<ASNNumber>
<Reference>
<RefNum>0805548867</RefNum>
</Reference>
</ASNNumber>
<OtherInvoiceReferences>
<ListOfReferenceCoded>
<ReferenceCoded>
<ReferenceTypeCoded/>
<PrimaryReference>
<Reference>
<RefNum>0006543680</RefNum>
</Reference>
</PrimaryReference>
<SupportingReference>
<Reference>
<RefNum>000003</RefNum>
</Reference>
</SupportingReference>
</ReferenceCoded>
</ListOfReferenceCoded>
</OtherInvoiceReferences>
</InvoiceReferences>
</LineItemReferences>
</InvoiceBaseItemDetail>
<InvoicePricingDetail>
<ListOfPrice>
<Price>
<UnitPrice>
<UnitPriceValue>119.79</UnitPriceValue>
</UnitPrice>
</Price>
</ListOfPrice>
<Tax>
<TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded>
<TaxCategoryCoded>StandardRate</TaxCategoryCoded>
<TaxTypeCoded>Other</TaxTypeCoded>
<TaxTypeCodedOther>
<Identifier>
<Agency>
<AgencyCoded>Other</AgencyCoded>
<AgencyCodedOther>Not Specified</AgencyCodedOther>
</Agency>
<Ident>National Tax</Ident>
</Identifier>
</TaxTypeCodedOther>
<TaxPercent>10.000</TaxPercent>
<TaxAmount>450.79</TaxAmount>
</Tax>
<InvoiceCurrencyTotalValue>
<MonetaryValue>
<MonetaryAmount>4507.94</MonetaryAmount>
</MonetaryValue>
</InvoiceCurrencyTotalValue>
</InvoicePricingDetail>
</InvoiceItemDetail>
<InvoiceItemDetail>
<InvoiceBaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>000003</BuyerLineItemNum>
<SellerLineItemNum>000003</SellerLineItemNum>
</LineItemNum>
<LineItemType>
<LineItemTypeCoded>Item</LineItemTypeCoded>
</LineItemType>
<ItemIdentifiers>
<PartNumbers>
<SellerPartNumber>
<PartNum>
<PartID>100356381</PartID>
</PartNum>
</SellerPartNumber>
<BuyerPartNumber>
<PartNum>
<PartID>517801</PartID>
</PartNum>
</BuyerPartNumber>
</PartNumbers>
</ItemIdentifiers>
<TotalQuantity>
<Quantity>
<QuantityValue>60480.0</QuantityValue>
<UnitOfMeasurement>
<UOMCoded>EA</UOMCoded>
</UnitOfMeasurement>
</Quantity>
</TotalQuantity>
<LineItemReferences>
<InvoiceReferences>
<PurchaseOrderReference>
<PurchaseOrderNumber>
<Reference>
<RefNum>0045486186</RefNum>
</Reference>
</PurchaseOrderNumber>
<PurchaseOrderDate>20110825T040000.000Z</PurchaseOrderDate>
<PurchaseOrderLineItemNumber>000003</PurchaseOrderLineItemNumber>
</PurchaseOrderReference>
<ASNNumber>
<Reference>
<RefNum>0805548867</RefNum>
</Reference>
</ASNNumber>
<OtherInvoiceReferences>
<ListOfReferenceCoded>
<ReferenceCoded>
<ReferenceTypeCoded/>
<PrimaryReference>
<Reference>
<RefNum>0006543680</RefNum>
</Reference>
</PrimaryReference>
<SupportingReference>
<Reference>
<RefNum>000004</RefNum>
</Reference>
</SupportingReference>
</ReferenceCoded>
</ListOfReferenceCoded>
</OtherInvoiceReferences>
</InvoiceReferences>
</LineItemReferences>
</InvoiceBaseItemDetail>
<InvoicePricingDetail>
<ListOfPrice>
<Price>
<UnitPrice>
<UnitPriceValue>115.11</UnitPriceValue>
</UnitPrice>
</Price>
</ListOfPrice>
<Tax>
<TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded>
<TaxCategoryCoded>StandardRate</TaxCategoryCoded>
<TaxTypeCoded>Other</TaxTypeCoded>
<TaxTypeCodedOther>
<Identifier>
<Agency>
<AgencyCoded>Other</AgencyCoded>
<AgencyCodedOther>Not Specified</AgencyCodedOther>
</Agency>
<Ident>National Tax</Ident>
</Identifier>
</TaxTypeCodedOther>
<TaxPercent>10.000</TaxPercent>
<TaxAmount>696.19</TaxAmount>
</Tax>
<InvoiceCurrencyTotalValue>
<MonetaryValue>
<MonetaryAmount>6961.85</MonetaryAmount>
</MonetaryValue>
</InvoiceCurrencyTotalValue>
</InvoicePricingDetail>
</InvoiceItemDetail>
<InvoiceItemDetail>
<InvoiceBaseItemDetail>
<LineItemNum>
<BuyerLineItemNum>000001</BuyerLineItemNum>
<SellerLineItemNum>000004</SellerLineItemNum>
</LineItemNum>
<LineItemType>
<LineItemTypeCoded>Item</LineItemTypeCoded>
</LineItemType>
<ItemIdentifiers>
<PartNumbers>
<SellerPartNumber>
<PartNum>
<PartID>100351756</PartID>
</PartNum>
</SellerPartNumber>
<BuyerPartNumber>
<PartNum>
<PartID>462088</PartID>
</PartNum>
</BuyerPartNumber>
</PartNumbers>
</ItemIdentifiers>
<TotalQuantity>
<Quantity>
<QuantityValue>56448.0</QuantityValue>
<UnitOfMeasurement>
<UOMCoded>EA</UOMCoded>
</UnitOfMeasurement>
</Quantity>
</TotalQuantity>
<LineItemReferences>
<InvoiceReferences>
<PurchaseOrderReference>
<PurchaseOrderNumber>
<Reference>
<RefNum>0045486186</RefNum>
</Reference>
</PurchaseOrderNumber>
<PurchaseOrderDate>20110825T040000.000Z</PurchaseOrderDate>
<PurchaseOrderLineItemNumber>000001</PurchaseOrderLineItemNumber>
</PurchaseOrderReference>
<ASNNumber>
<Reference>
<RefNum>0805548867</RefNum>
</Reference>
</ASNNumber>
<OtherInvoiceReferences>
<ListOfReferenceCoded>
<ReferenceCoded>
<ReferenceTypeCoded/>
<PrimaryReference>
<Reference>
<RefNum>0006543680</RefNum>
</Reference>
</PrimaryReference>
<SupportingReference>
<Reference>
<RefNum>000005</RefNum>
</Reference>
</SupportingReference>
</ReferenceCoded>
</ListOfReferenceCoded>
</OtherInvoiceReferences>
</InvoiceReferences>
</LineItemReferences>
</InvoiceBaseItemDetail>
<InvoicePricingDetail>
<ListOfPrice>
<Price>
<UnitPrice>
<UnitPriceValue>119.79</UnitPriceValue>
</UnitPrice>
</Price>
</ListOfPrice>
<Tax>
<TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded>
<TaxCategoryCoded>StandardRate</TaxCategoryCoded>
<TaxTypeCoded>Other</TaxTypeCoded>
<TaxTypeCodedOther>
<Identifier>
<Agency>
<AgencyCoded>Other</AgencyCoded>
<AgencyCodedOther>Not Specified</AgencyCodedOther>
</Agency>
<Ident>National Tax</Ident>
</Identifier>
</TaxTypeCodedOther>
<TaxPercent>10.000</TaxPercent>
<TaxAmount>676.19</TaxAmount>
</Tax>
<InvoiceCurrencyTotalValue>
<MonetaryValue>
<MonetaryAmount>6761.91</MonetaryAmount>
</MonetaryValue>
</InvoiceCurrencyTotalValue>
</InvoicePricingDetail>
</InvoiceItemDetail>
</ListOfInvoiceItemDetail>
</InvoiceDetail>
<InvoiceSummary>
<NumberOfLines>4.0</NumberOfLines>
<InvoiceTotals>
<NetValue>
<MonetaryValue>
<MonetaryAmount>22872.94</MonetaryAmount>
</MonetaryValue>
</NetValue>
<GrossValue>
<MonetaryValue>
<MonetaryAmount>25160.23</MonetaryAmount>
</MonetaryValue>
</GrossValue>
<TaxValue>
<MonetaryValue>
<MonetaryAmount>2287.29</MonetaryAmount>
</MonetaryValue>
</TaxValue>
<TotalAmountPayable>
<MonetaryValue>
<MonetaryAmount>25160.23</MonetaryAmount>
</MonetaryValue>
</TotalAmountPayable>
</InvoiceTotals>
<ListOfTaxSummary>
<TaxSummary>
<Tax>
<TaxFunctionQualifierCoded>Tax</TaxFunctionQualifierCoded>
<TaxCategoryCoded>StandardRate</TaxCategoryCoded>
<TaxTypeCoded>Other</TaxTypeCoded>
<TaxTypeCodedOther>
<Identifier>
<Agency>
<AgencyCoded>Other</AgencyCoded>
<AgencyCodedOther>Not specified</AgencyCodedOther>
</Agency>
<Ident>NationalTax</Ident>
</Identifier>
</TaxTypeCodedOther>
<TaxPercent>10.000</TaxPercent>
<TaxAmount>2287.29</TaxAmount>
</Tax>
</TaxSummary>
</ListOfTaxSummary>
</InvoiceSummary>
</Invoice>
和out put是
<?xml version="1.0" encoding="UTF-8"?>
<INVOIC01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<E1EDP01 SEGMENT="1">
<POSEX>000002</POSEX>
<MENGE>40320.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>37632.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>119.79</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000003</POSEX>
<MENGE>60480.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>56448.0</MENGE>
<MENEE>EA</MENEE>
</E1EDP01>
</IDOC>
</INVOIC01>
很抱歉,如果文件看起来很奇怪。这是我第一次发送输出文件,你可以在POSEX和MENGE中看到E1EDP01。我需要删除POSEX中的重复值(EX应该是000001,并且应该添加MENGE值)
我希望这是有道理的。
答案 0 :(得分:1)
此转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kByP" match="E1EDP01" use="POSEX"/>
<xsl:template match="node()|@*">
<xsl:param name="pNewValue"/>
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:with-param name="pNewValue" select="$pNewValue"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match=
"E1EDP01[generate-id()
=
generate-id(key('kByP', POSEX)[1])
]">
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:with-param name="pNewValue" select=
"sum(key('kByP', POSEX)/MENGE)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="E1EDP01"/>
<xsl:template match="MENGE/text()">
<xsl:param name="pNewValue"/>
<xsl:value-of select="$pNewValue"/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档(略微更正以使其格式正确):
<INVOIC01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1"/>
<E1EDP01 SEGMENT="1">
<POSEX>000002</POSEX>
<MENGE>40320.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>37632.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>119.79</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000003</POSEX>
<MENGE>60480.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>56448.0</MENGE>
<MENEE>EA</MENEE>
</E1EDP01>
</IDOC>
</INVOIC01>
生成想要的正确结果:
<INVOIC01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1"/>
<E1EDP01 SEGMENT="1">
<POSEX>000002</POSEX>
<MENGE>40320</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>94080</MENGE>
<MENEE>EA</MENEE>
<VPREI>119.79</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000003</POSEX>
<MENGE>60480</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
</IDOC>
</INVOIC01>
<强>解释强>:
身份规则“按原样”复制每个节点。我添加了一个名为pNewValue
的参数,该参数仅供与任何文本节点子项匹配的模板使用MENGE
- 提供要输出的新值(组中所有MENGE
的总和)。
使用Muenchian分组方法。
与@empo的解决方案不同,我们不需要知道其他元素的名称及其顺序:)
答案 1 :(得分:0)
所以你想从iDoc XML中删除重复的POSEX?看起来像是因为你使用
<xsl:template match="E1EDP01[not(generate-id() = generate-id(key('kPOSEX', POSEX)[000001]))]"/>
我稍微调整了一下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="kPOSEX" match="E1EDP01" use="POSEX"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="E1EDP01[not(generate-id() = generate-id(key('kPOSEX', POSEX)[1]))]">
</xsl:template>
</xsl:stylesheet>
并应用于您的iDoc XML示例我得到了这个输出:
<?xml version="1.0" encoding="UTF-8"?><INVOIC01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1"/>
<E1EDP01 SEGMENT="1">
<POSEX>000002</POSEX>
<MENGE>40320.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>37632.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>119.79</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000003</POSEX>
<MENGE>60480.0</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
</IDOC>
</INVOIC01>
删除副本(POSEX = 000001)。 那是你想要的吗?
此致 彼得
答案 2 :(得分:0)
我需要删除POSEX中的重复值(EX应该是000001,并且应该添加MENGE值)
您希望 POSEX ,并为每个组添加 MENGE 的值。
以下转换执行任务:
我还按 POSEX 排序了元素。
[XSLT 1.0]
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kPOSEX" match="E1EDP01" use="POSEX"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="EDI_DC40">
<xsl:copy>
<xsl:apply-templates select="@*
|
E1EDP01[generate-id()
= generate-id(key('kPOSEX', POSEX)[1])]">
<xsl:sort select="POSEX" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="E1EDP01">
<xsl:copy>
<xsl:apply-templates select="@*|POSEX"/>
<MENGE>
<xsl:value-of select="sum(key('kPOSEX',POSEX)/MENGE)"/>
</MENGE>
<xsl:apply-templates select="MENEE|VPREI"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
带输出:
<INVOIC01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<E1EDP01 SEGMENT="1">
<POSEX>000001</POSEX>
<MENGE>94080</MENGE>
<MENEE>EA</MENEE>
<VPREI>119.79</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000002</POSEX>
<MENGE>40320</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>000003</POSEX>
<MENGE>60480</MENGE>
<MENEE>EA</MENEE>
<VPREI>115.11</VPREI>
</E1EDP01>
</EDI_DC40>
</IDOC>
</INVOIC01>