如何使用 XSLT 获取组中每一行的小计

问题描述 投票:0回答:1

我有一个特殊要求,要在组的单独行中显示小计。我尝试列出每一行,并使用我按供应商发票编号分组的 for-each-group 函数。但结果仅显示单个行值而不是小计金额。你们能在这里展示一些关于我们如何实现这种要求的简单内容吗?

Sample Records:

    <Report_Data>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-01</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>1</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-01</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>2</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>-100</Amount>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-02</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>1</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-02</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>2</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
        </Report_Entry>
    </Report_Data>

Expected Output:
            <Report_Data>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-01</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>1</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
            <SubTotal>-90</SubTotal>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-01</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>2</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>-100</Amount>
            <SubTotal>-90</SubTotal>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-02</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>1</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
            <SubTotal>20</SubTotal>
        </Report_Entry>
        <Report_Entry>
            <Supplier_Invoice_Number>SINV-02</Supplier_Invoice_Number>
            <Supplier_Invoice_Line>2</Supplier_Invoice_Line>
            <Memo>SC0590</Memo>
            <Amount>10</Amount>
            <SubTotal>20</SubTotal>
        </Report_Entry>
    </Report_Data>

XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="2.0">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="Report_Data">
            <Report_Data>
                <xsl:for-each select="Report_Entry">
                    <Report_Entry>
                        <xsl:copy-of select="Supplier_Invoice_Number"/>
                        <xsl:copy-of select="Supplier_Invoice_Line"/>
                        <xsl:copy-of select="Memo"/>
                        <xsl:copy-of select="Amount"/>
                        <SubTotal>
                            <xsl:for-each-group select="." group-by="Supplier_Invoice_Number">
                            <xsl:value-of select="sum(Amount)"/>
                            </xsl:for-each-group>
                        </SubTotal>
                    </Report_Entry>
                </xsl:for-each>
            </Report_Data>
            
        </xsl:template>
        
    </xsl:stylesheet>
xml xslt
1个回答
0
投票

我认为这会产生你期望的输出:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="Report_Data">
    <Report_Data>
        <xsl:for-each-group select="Report_Entry" group-by="Supplier_Invoice_Number">
            <xsl:variable name="subtotal" select="sum(current-group()/Amount)" />
            <xsl:for-each select="current-group()">
                <Report_Entry>
                    <xsl:copy-of select="*"/>
                    <SubTotal>
                        <xsl:value-of select="$subtotal"/>
                    </SubTotal>
                </Report_Entry>
            </xsl:for-each>
        </xsl:for-each-group>
    </Report_Data>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.