【问题标题】:XSLT 2.0 - High Level Grouping with Sums (WordML)XSLT 2.0 - 使用总和的高级分组 (WordML)
【发布时间】:2017-10-26 06:05:27
【问题描述】:

尊敬的专业人士,

我有一些联合计费 XML 代码,其中包含时间卡/计时员摘要。我可以轻松地按特定事项分组,然后按计时员分组并获得每个事项的总数。但我需要得到计时员对整个联合账单的所有小时数和计费金额的总和,而不仅仅是每件事情。

我在下面引用了所有代码,但更新自 @Tim-C 的评论。 XSL 转换链接是HERE

输入:

<?xml version="1.0" encoding="utf-8"?>
<superbill>
    <invoice type="P" id="562845" number="562845">
        <matters>
            <matter number="014592-000007">
                <timecard-summary-by-timekeeper>
                    <timekeeper-summary timekeeper-id="NC1">
                        <timekeeper>
                            <initials>NC1</initials>
                            <billingname>Nicholas J. Collins</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">525.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                    <timekeeper-summary timekeeper-id="BJB">
                        <timekeeper>
                            <initials>BJB</initials>
                            <billingname>Billie J. Bob</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">575.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                </timecard-summary-by-timekeeper>
            </matter>
            <matter number="014592-000091">
                <timecard-summary-by-timekeeper>
                    <timekeeper-summary timekeeper-id="NC1">
                        <timekeeper>
                            <initials>NC1</initials>
                            <billingname>Nicholas J. Collins</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">525.00</total>
                                <total type="hours">1.00</total>
                                <total type="amount">525.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">525.00</total>
                                <total type="hours">1.00</total>
                                <total type="amount">525.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">525.00</total>
                                <total type="hours">1.00</total>
                                <total type="amount">525.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">525.00</total>
                                <total type="hours">1.00</total>
                                <total type="amount">525.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">1.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                    <timekeeper-summary timekeeper-id="BJB">
                        <timekeeper>
                            <initials>BJB</initials>
                            <billingname>Billie J. Bob</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">575.00</total>
                                <total type="hours">11.00</total>
                                <total type="amount">6325.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">575.00</total>
                                <total type="hours">11.00</total>
                                <total type="amount">6325.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">575.00</total>
                                <total type="hours">11.00</total>
                                <total type="amount">6325.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">575.00</total>
                                <total type="hours">11.00</total>
                                <total type="amount">6325.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">11.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                </timecard-summary-by-timekeeper>
            </matter>
            <matter number="014592-000092">
                <timecard-summary-by-timekeeper>
                    <timekeeper-summary timekeeper-id="NC1">
                        <timekeeper>
                            <initials>NC1</initials>
                            <billingname>Nicholas J. Collins</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">525.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                    <timekeeper-summary timekeeper-id="BJB">
                        <timekeeper>
                            <initials>BJB</initials>
                            <billingname>Billie J. Bob</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">575.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                </timecard-summary-by-timekeeper>
            </matter> 
            <matter number="014592-000095">
                <timecard-summary-by-timekeeper>
                    <timekeeper-summary timekeeper-id="NC1">
                        <timekeeper>
                            <initials>NC1</initials>
                            <billingname>Nicholas J. Collins</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">525.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2100.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">525.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2100.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">525.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2100.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">525.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2100.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                    <timekeeper-summary timekeeper-id="BJB">
                        <timekeeper>
                            <initials>BJB</initials>
                            <billingname>Billie J. Bob</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">575.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">0.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                </timecard-summary-by-timekeeper>
            </matter>
            <matter number="014592-000096">
                <timecard-summary-by-timekeeper>
                    <timekeeper-summary timekeeper-id="NC1">
                        <timekeeper>
                            <initials>NC1</initials>
                            <billingname>Nicholas J. Collins</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">525.00</total>
                                <total type="hours">20.00</total>
                                <total type="amount">10500.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">525.00</total>
                                <total type="hours">20.00</total>
                                <total type="amount">10500.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">525.00</total>
                                <total type="hours">20.00</total>
                                <total type="amount">10500.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">525.00</total>
                                <total type="hours">20.00</total>
                                <total type="amount">10500.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">20.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                    <timekeeper-summary timekeeper-id="BJB">
                        <timekeeper>
                            <initials>BJB</initials>
                            <billingname>Billie J. Bob</billingname>
                        </timekeeper>
                        <timekeeper-summary-totals>
                            <timekeeper-summary-total type="billed">
                                <total type="rate">575.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2300.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="worked">
                                <total type="rate">575.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2300.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="standard">
                                <total type="rate">575.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2300.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate1">
                                <total type="rate">575.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">2300.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                            <timekeeper-summary-total type="timerate2">
                                <total type="rate">0.00</total>
                                <total type="hours">4.00</total>
                                <total type="amount">0.00</total>
                                <total type="adjustment">0.00</total>
                            </timekeeper-summary-total>
                        </timekeeper-summary-totals>
                    </timekeeper-summary>
                </timecard-summary-by-timekeeper>
            </matter>
        </matters>
    </invoice>
</superbill>

当前代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
               xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
                version="2.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <xsl:call-template name="XJ_TKSummary_02" />
    </xsl:template>

    <xsl:template name="XJ_TKSummary_02">
       <xsl:variable name="TW" select="1440" />
        <xsl:for-each-group select="//superbill/invoice/matters/matter/timecard-summary-by-timekeeper/timekeeper-summary/timekeeper" group-by="../@timekeeper-id">
            <w:p>
                <w:r>
                    <w:t><xsl:value-of select="initials" /> - <xsl:value-of select="billingname" /></w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:r>
                    <xsl:variable name="groupTotals" select="current-group()/timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total" />
                    <w:t>
                        Hours: <xsl:value-of select="format-number(sum($groupTotals[@type='hours']), '###,##0.00')" />
                        for $ <xsl:value-of select="format-number(sum($groupTotals[@type='amount']), '###,##0.00')" />
                    </w:t>
                </w:r>
            </w:p>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

输出:

所需的总数应为...

NC1 - Nicholas J. Collins
Hours: 25.00 for $13,125.00

BJB - Billie J. Bob
Hours: 15.00 for $8,625.00

我们将不胜感激。

注意,

-尼克

【问题讨论】:

    标签: xml xslt xslt-2.0 wordml


    【解决方案1】:

    您需要做的主要事情是将xsl:for-each-group 更改为此...

    <xsl:for-each-group select="//invoice/matter/timecard-summary-by-timekeeper/timekeeper-summary/timekeeper" group-by="../@timekeeper-id">
    

    因此,您将 timekeeper 记录按其父 timekeeper-id 属性分组。

    您还需要在 xpath 中包含 timekeeper-summary-totals 以获取您的总小时数和金额。

    <w:t>
      Hours: 
     <xsl:value-of select="format-number(sum(current-group()/timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total[@type='hours']), '###,##0.00')" />
     for $
     <xsl:value-of select="format-number(sum(current-group()/timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total[@type='amount']), '###,##0.00')" />
    </w:t>
    

    或者,更好的是,使用变量来减少 xpath 的代码重复

    试试这个精​​简的 XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                   xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
                    version="2.0">
        <xsl:output method="xml" indent="yes" />
    
        <xsl:template match="/">
            <xsl:call-template name="XJ_TKSummary_02" />
        </xsl:template>
    
        <xsl:template name="XJ_TKSummary_02">
           <xsl:variable name="TW" select="1440" />
            <xsl:for-each-group select="//invoice/matter/timecard-summary-by-timekeeper/timekeeper-summary/timekeeper" group-by="../@timekeeper-id">
                <w:p>
                    <w:r>
                        <w:t><xsl:value-of select="initials" /> - <xsl:value-of select="billingname" /></w:t>
                    </w:r>
                </w:p>
                <w:p>
                    <w:r>
                        <xsl:variable name="groupTotals" select="current-group()/timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total" />
                        <w:t>
                            Hours: <xsl:value-of select="format-number(sum($groupTotals[@type='hours']), '###,##0.00')" />
                            for $ <xsl:value-of select="format-number(sum($groupTotals[@type='amount']), '###,##0.00')" />
                        </w:t>
                    </w:r>
                </w:p>
            </xsl:for-each-group>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 谢谢!我得到了每个计时员,但小时数和金额都是 0.00。
    • 刚刚注意到它只考虑了第一件事。刚刚测试了将第 4 和第 5 计时员添加到 XML 中的第 2 件事中,它只列出了 3 个原始计时员,而不是将 # 加起来。有些东西使它们保持在 0.00。
    • @NCollinsTE,考虑编辑您的问题并提供最小字节完整输入示例来演示问题。当我尝试使用您当前的 sn-p 时,蒂姆在 xsltransform.net/a9GiwC 的代码似乎输出了您迄今为止描述的值。
    • 在该示例中,结构有点不同,所以我认为您想将 &lt;xsl:variable name="groupTotals" select="current-group()/timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total" /&gt; 更改为 &lt;xsl:variable name="groupTotals" select="current-group()/../timekeeper-summary-totals/timekeeper-summary-total[@type='billed']/total" /&gt;
    • 作为替代方案,您可以调整分组人口,就像我在 xsltransform.net/3MvmrzE/2 中所做的那样,例如&lt;xsl:for-each-group select="//superbill/invoice/matters/matter/timecard-summary-by-timekeeper/timekeeper-summary" group-by="@timekeeper-id"&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2011-07-02
    • 2013-11-09
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多