XML 分组 Muenchian 方法问题



尝试在XSLT 1.0版本中进行分组,但有些东西不起作用,我看不出到底出了什么问题。我需要按增值税代码分组,并在发票中按同一增值税代码对所有金额求和。有什么帮助吗?添加 XML(由 2 张纸添加,但它们在一个文件中作为 2 个不同的行(和 XSL。也许有人可以帮助我,看看XML和XSL?

.XML:

    <E_Invoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
    <Test>YES</Test>
    <Date>2018-03-05</Date>
    <FileId>dst05.03.18 12:02:34</FileId>
    <Version>1.2</Version>
    <ReceiverId>111529447</ReceiverId>
</Header>
    <Invoice regNumber="111529447" invoiceId="63">
    <InvoiceItem>
        <InvoiceItemGroup>
            <ItemEntry>
                <Accounting>
                    <Description>KR0002202</Description>
                    <JournalEntry>
                        <GeneralLedger>61181</GeneralLedger>
                        <CostObjective>
                            <Description/>
                            <VatCode>08</VatCode>
                            <AccountingDate/>
                            <PurchaseLedger/>
                            <DIM03>290</DIM03>
                            <DIM02>03TPRO</DIM02>
                        </CostObjective>
                        <Sum>9953.4000</Sum>
                        <VatSum>2090.2100</VatSum>
                        <VatRate>21.0000</VatRate>
                    </JournalEntry>
                </Accounting>
                <Description>KR0002202</Description>
                <ItemSum>9953.4000</ItemSum>
                <VAT>
                    <SumBeforeVAT>9953.4000</SumBeforeVAT>
                    <VATRate>21.0000</VATRate>
                    <VATSum>2090.2100</VATSum>
                    <Currency>EUR</Currency>
                </VAT>
                <ItemTotal>12043.6100</ItemTotal>
            </ItemEntry>
        </InvoiceItemGroup>
    </InvoiceItem>
</Invoice>
<Invoice regNumber="111529447" invoiceId="65">
    <InvoiceItem>
        <InvoiceItemGroup>
            <ItemEntry>
                <Accounting>
                    <Description>LP2018000033</Description>
                    <JournalEntry>
                        <GeneralLedger>10090</GeneralLedger>
                        <CostObjective>
                            <Description/>
                            <VatCode>08</VatCode>
                            <AccountingDate/>
                            <PurchaseLedger/>
                            <DIM02>03TPRO</DIM02>
                            <DIM03>283</DIM03>
                            <DIM04>COM</DIM04>
                            <DIM01>01</DIM01>
                        </CostObjective>
                        <Sum>466.9400</Sum>
                        <VatSum>98.0600</VatSum>
                        <VatRate>21.0000</VatRate>
                    </JournalEntry>
                </Accounting>
                <Description>LP2018000033</Description>
                <ItemSum>466.9400</ItemSum>
                <VAT>
                    <SumBeforeVAT>466.9400</SumBeforeVAT>
                    <VATRate>21.0000</VATRate>
                    <VATSum>98.0600</VATSum>
                    <Currency>EUR</Currency>
                </VAT>
                <ItemTotal>565.0000</ItemTotal>
            </ItemEntry>
            <ItemEntry>
                <Accounting>
                    <Description>21 %</Description>
                    <JournalEntry>
                        <GeneralLedger>10000</GeneralLedger>
                        <CostObjective>
                            <Description/>
                            <VatCode>02</VatCode>
                            <AccountingDate/>
                            <PurchaseLedger/>
                            <DIM02>ADMHR</DIM02>
                            <DIM03>289</DIM03>
                            <DIM04>MET</DIM04>
                            <DIM01>02</DIM01>
                        </CostObjective>
                        <Sum>1210.0000</Sum>
                        <VatSum>0.0000</VatSum>
                        <VatRate>0.0000</VatRate>
                    </JournalEntry>
                </Accounting>
                <Description>21 %</Description>
                <ItemSum>1210.0000</ItemSum>
                <VAT>
                    <SumBeforeVAT>1210.0000</SumBeforeVAT>
                    <VATRate>0.0000</VATRate>
                    <VATSum>0.0000</VATSum>
                    <Currency>EUR</Currency>
                </VAT>
                <ItemTotal>1210.0000</ItemTotal>
            </ItemEntry>
        </InvoiceItemGroup>
    </InvoiceItem>

XSL: -->

<xsl:key name="sum-by-vatcode" match="ItemEntry"
use="concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode)"/>
    <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>
    <xsl:template match="Invoice">
    <agency>
        <xsl:for-each select=
        "ItemEntry[generate-id()=generate-id(key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))[1])]">
        <xsl:variable name="vkeyGroup" select=
        "key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))"/>
        <ItemEntry>
        <xsl:copy-of select="*[starts-with(name(), 'key')]"/>
        <VATSum>
        <xsl:value-of select="sum($vkeyGroup/VAT/VATSum)"/>
        </VATSum>
        <Sum>
        <xsl:value-of select="sum($vkeyGroup/VAT/SumBeforeVAT)"/>
        </Sum>
        </ItemEntry>
        </xsl:for-each>
    </agency>
    </xsl:template>

更正路径

<xsl:for-each select=
        "ItemEntry[generate-id()=generate-id(key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))[1])]">

<xsl:for-each select=
        ".//ItemEntry[generate-id()=generate-id(key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))[1])]">

至少有机会选择模板的Invoice上下文节点的ItemEntry后代。

随着 https://xsltfiddle.liberty-development.net/bFDb2BR 的变化,我得到了输出

<E_Invoice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <Test>YES</Test>
    <Date>2018-03-05</Date>
    <FileId>dst05.03.18 12:02:34</FileId>
    <Version>1.2</Version>
    <ReceiverId>111529447</ReceiverId>
  </Header>
  <agency>
    <ItemEntry>
      <VATSum>2090.21</VATSum>
      <Sum>9953.4</Sum>
    </ItemEntry>
  </agency>
  <agency>
    <ItemEntry>
      <VATSum>98.06</VATSum>
      <Sum>466.94</Sum>
    </ItemEntry>
    <ItemEntry>
      <VATSum>0</VATSum>
      <Sum>1210</Sum>
    </ItemEntry>
  </agency>
</E_Invoice>

对于代码

<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="sum-by-vatcode" match="ItemEntry"
use="concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode)"/>
    <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>
    <xsl:template match="Invoice">
    <agency>
        <xsl:for-each select=
        ".//ItemEntry[generate-id()=generate-id(key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))[1])]">
        <xsl:variable name="vkeyGroup" select=
        "key('sum-by-vatcode', concat(generate-id(..), Accounting/JournalEntry/CostObjective/VatCode))"/>
        <ItemEntry>
        <xsl:copy-of select="*[starts-with(name(), 'key')]"/>
        <VATSum>
        <xsl:value-of select="sum($vkeyGroup/VAT/VATSum)"/>
        </VATSum>
        <Sum>
        <xsl:value-of select="sum($vkeyGroup/VAT/SumBeforeVAT)"/>
        </Sum>
        </ItemEntry>
        </xsl:for-each>
    </agency>
    </xsl:template>
</xsl:stylesheet>

最新更新