XSL 1.0 分组依据和总和销售额和相关/联接的税务记录



我有一个发票xml,我需要对itemid的 例如,对于 ItemId 444,我需要 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>

我不知道这是否可能。更不用说从哪里开始了?

话筒

您可以将CustInvoiceTrans元素分组两次,一次用于对它们的组件进行分组和汇总,第二次用于创建引用的TaxTrans元素的总和,您只需使用键<xsl:key name="RefTaxTrans" match="TaxTrans" use="InventTransId"/>找到这些元素即可; 下面显示了如何创建分组TaxTrans并计算TaxAmount的总和,您只需要分别复制其他元素对该模板内的其他元素求和TaxTrans结果元素:

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

https://xsltfiddle.liberty-development.net/jyH9rMG

然后,您可以将 CustInvoiceTrans 元素的正常分组与默认模式下的另一个apply-templates添加到<xsl:apply-templates select="CustInvoiceTrans[generate-id() = generate-id(key('CustInvoiceTrans', ItemId)[1])]" mode="tax"/>行之前。或者,存储每个组的第一项,然后应用模板两次可能更容易,一次在默认的未命名模式下,第二次在引用项的模式下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<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:variable name="group-heads"
select="CustInvoiceTrans[generate-id() = generate-id(key('CustInvoiceTrans', ItemId)[1])]"/>
<xsl:apply-templates select="$group-heads"/>
<xsl:apply-templates select="$group-heads" mode="tax"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CustInvoiceTrans/LineAmount">
<xsl:copy>
<xsl:value-of select="sum(key('CustInvoiceTrans', ../ItemId)/LineAmount)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CustInvoiceTrans" mode="tax">
<TaxTrans class="entity">
<xsl:variable name="referenced-tax" select="key('RefTaxTrans', key('CustInvoiceTrans', ItemId)/InventTransId)"/>
<xsl:copy-of select="$referenced-tax[1]/InventTransId"/>
<TaxAmount>
<xsl:value-of select="sum($referenced-tax/TaxAmount)"/>
</TaxAmount>
<TaxBaseAmount>
<xsl:value-of select="sum($referenced-tax/TaxBaseAmount)"/>
</TaxBaseAmount>
<xsl:copy-of select="$referenced-tax[1]/TaxValue"/>
</TaxTrans>
</xsl:template>
</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/jyH9rMG/1

相关内容

  • 没有找到相关文章

最新更新