我有一个发票xml,我需要对itemid进行分组/总和 例如,对于ItemId 444,我需要一个lineamount为100 + 25 = 125,而ItemId 555 = 200 +15。您将要说的很多示例是如何执行的,但是我还有另一个要求。我还需要根据CustInvoiceTrans和TaxTrans之间加入的InventTransId字段(AABB / BBCC)对TaxTrans \ TaxAmounts和TaxTrans \ TaxBaseAmounts进行相同的分组和总和。
之前:
<CustInvoiceJour class="entity">
<CustInvoiceTrans class="entity">
<InventTransId>AABB</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>444</ItemId>
<LineAmount>100</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>BBCC</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>444</ItemId>
<LineAmount>25</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>CCDD</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>555</ItemId>
<LineAmount>200</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>DDEE</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>555</ItemId>
<LineAmount>15</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>EEFF</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>12345</ItemId>
<LineAmount>40</LineAmount>
</CustInvoiceTrans>
<TaxTrans class="entity">
<InventTransId>AABB</InventTransId>
<TaxAmount>-21</TaxAmount>
<TaxBaseAmount>-100</TaxBaseAmount>
<TaxValue>21.00</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>BBCC</InventTransId>
<TaxAmount>-5.25</TaxAmount>
<TaxBaseAmount>-25</TaxBaseAmount>
<TaxValue>21.00</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>CCDD</InventTransId>
<TaxAmount>-42</TaxAmount>
<TaxBaseAmount>-200</TaxBaseAmount>
<TaxValue>21.00</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>DDEE</InventTransId>
<TaxAmount>-3.15</TaxAmount>
<TaxBaseAmount>-15</TaxBaseAmount>
<TaxValue>15</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>EEFF</InventTransId>
<TaxAmount>-8.40</TaxAmount>
<TaxBaseAmount>-40</TaxBaseAmount>
<TaxValue>15</TaxValue>
</TaxTrans>
</CustInvoiceJour>
必填输出:
<CustInvoiceJour class="entity">
<CustInvoiceTrans class="entity">
<InventTransId>AABB</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>444</ItemId>
<LineAmount>125</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>CCDD</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>555</ItemId>
<LineAmount>215</LineAmount>
</CustInvoiceTrans>
<CustInvoiceTrans class="entity">
<InventTransId>EEFF</InventTransId>
<InvoiceId>SI100</InvoiceId>
<ItemId>12345</ItemId>
<LineAmount>40</LineAmount>
</CustInvoiceTrans>
<TaxTrans class="entity">
<InventTransId>AABB</InventTransId>
<TaxAmount>-26.25</TaxAmount>
<TaxBaseAmount>-125</TaxBaseAmount>
<TaxValue>21.00</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>CCDD</InventTransId>
<TaxAmount>-45.15</TaxAmount>
<TaxBaseAmount>-215</TaxBaseAmount>
<TaxValue>21.00</TaxValue>
</TaxTrans>
<TaxTrans class="entity">
<InventTransId>EEFF</InventTransId>
<TaxAmount>-8.40</TaxAmount>
<TaxBaseAmount>-40</TaxBaseAmount>
<TaxValue>15</TaxValue>
</TaxTrans>
</CustInvoiceJour>
我不知道这是否有可能。更alone论从哪里开始?
迈克
答案 0 :(得分:2)
您可以将CustInvoiceTrans
元素进行两次分组,一次是对它们的成分进行分组和汇总,第二次是创建引用的TaxTrans
元素的总和,而您只需使用键{{1} };下面显示了如何创建分组的<xsl:key name="RefTaxTrans" match="TaxTrans" use="InventTransId"/>
并计算TaxTrans
的总和,您只需要复制其他元素,分别将模板中的其他元素与TaxAmount
结果元素相加:
TaxTrans
https://xsltfiddle.liberty-development.net/jyH9rMG
然后可以在默认模式下,在<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="CustInvoiceTrans" match="CustInvoiceTrans" use="ItemId"/>
<xsl:key name="RefTaxTrans" match="TaxTrans" use="InventTransId"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CustInvoiceJour">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="CustInvoiceTrans[generate-id() = generate-id(key('CustInvoiceTrans', ItemId)[1])]" mode="tax"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CustInvoiceTrans" mode="tax">
<TaxTrans>
<xsl:variable name="referenced-tax" select="key('RefTaxTrans', key('CustInvoiceTrans', ItemId)/InventTransId)"/>
<TaxAmount>
<xsl:value-of select="sum($referenced-tax/TaxAmount)"/>
</TaxAmount>
</TaxTrans>
</xsl:template>
</xsl:stylesheet>
行之前,使用另一个apply-templates
为CustInvoiceTrans元素添加常规分组。或者,可能更容易存储每个组的第一项,然后两次应用模板,一次是在默认的未命名模式下,第二次是在所引用项的模式下:
<xsl:apply-templates select="CustInvoiceTrans[generate-id() = generate-id(key('CustInvoiceTrans', ItemId)[1])]" mode="tax"/>