XSLT 2.0:分组依据和求和问题

XSLT 2.0: Group-By and Sum Issue

提问人:MAT0718 提问时间:11/2/2023 更新时间:11/2/2023 访问量:70

问:

这是我的源 XML

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
    <Worker>
        <empID>12354</empID>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Expense</Type>
            <Amount>108</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Bonus</Type>
            <Amount>200</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Expense</Type>
            <Amount>550</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-10-07:00</Date>
            <Type>Expense</Type>
            <Amount>100</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-10-07:00</Date>
            <Type>Bonus</Type>
            <Amount>200</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-10-07:00</Date>
            <Type>Expense</Type>
            <Amount>120</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-11-07:00</Date>
            <Type>Expense</Type>
            <Amount>120</Amount>
        </Cost>
    </Worker>
    <Worker>
        <empID>4567</empID>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Expense</Type>
            <Amount>90</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Bonus</Type>
            <Amount>10</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-06-07:00</Date>
            <Type>Expense</Type>
            <Amount>120</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-10-07:00</Date>
            <Type>Expense</Type>
            <Amount>1297</Amount>
        </Cost>
        <Cost>
            <Date>2023-10-10-07:00</Date>
            <Type>Bonus</Type>
            <Amount>312</Amount>
        </Cost>
    </Worker>
</Workers>

预期输出

  • 根据 和 对记录进行分组<empID><Date>
  • 创建与唯一值一样多的节点,因此我在第一个节点下有三个唯一值,其值为 .我的预期输出 XML 需要包含三个节点,因为有三个唯一值,例如Worker<Date><Date><Worker><empID>12354<Worker><empID>12354
  • 2023-10-06-07:00
  • 2023-10-10-07:00
  • 2023-10-11-07:00

和两个节点,因为有两个唯一的值,例如<Worker><empID>4567

  • 2023-10-06-07:00

  • 2023-10-10-07:00

  • 将唯一值的金额相加TypeExpense<Date>

  • 将唯一值的金额相加TypeBonus<Date>

预期结果

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
    <Worker>
        <UniqueID>12354</UniqueID>
        <Date>2023-10-06-07:00</Date>
        <ExpenseAmount>658.00</ExpenseAmount>
        <BonusAmount>200.00</BonusAmount>
    </Worker>
    <Worker>
        <UniqueID>12354</UniqueID>
        <Date>2023-10-10-07:00</Date>
        <ExpenseAmount>220.00</ExpenseAmount>
        <BonusAmount>200.00</BonusAmount>
    </Worker>
    <Worker>
        <UniqueID>12354</UniqueID>
        <Date>2023-10-11-07:00</Date>
        <ExpenseAmount/>
        <BonusAmount>120.00</BonusAmount>
    </Worker>
    <Worker>
        <UniqueID>4567</UniqueID>
        <Date>2023-10-06-07:00</Date>
        <ExpenseAmount>210.00</ExpenseAmount>
        <BonusAmount>10.00</BonusAmount>
    </Worker>
    <Worker>
        <UniqueID>4567</UniqueID>
        <Date>2023-10-10-07:00</Date>
        <ExpenseAmount>1297.00</ExpenseAmount>
        <BonusAmount>312.00</BonusAmount>
    </Worker>
</Workers>

这是我的 XSLT

<?xml version="1.0" encoding="UTF-8"?>
<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="Workers">
        <Workers>
            <xsl:for-each-group select="Worker" group-by="empID">
                <xsl:for-each-group select="current-group()" group-by="Cost/Date">
                    <Worker>
                        <UniqueID>
                            <xsl:value-of select="empID"/>
                        </UniqueID>
                        <Date>
                            <xsl:value-of select="current-grouping-key()"/>
                        </Date>
                        <ExpenseAmount>
                            <xsl:value-of
                                select="format-number(sum(current-group()/Cost[Type = 'Expense']/Amount), '#.00')"/>
                        </ExpenseAmount>
                        <BonusAmount>
                            <xsl:value-of select="format-number(sum(current-group()/Cost[Type = 'Bonus']/Amount), '#.00')"/>
                        </BonusAmount>
                    </Worker>
                </xsl:for-each-group>
            </xsl:for-each-group>
        </Workers>
    </xsl:template>
</xsl:stylesheet>

电流输出

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
   <Worker>
      <UniqueID>12354</UniqueID>
      <Date>2023-10-06-07:00</Date>
      <ExpenseAmount>998.00</ExpenseAmount>
      <BonusAmount>400.00</BonusAmount>
   </Worker>
   <Worker>
      <UniqueID>12354</UniqueID>
      <Date>2023-10-10-07:00</Date>
      <ExpenseAmount>998.00</ExpenseAmount>
      <BonusAmount>400.00</BonusAmount>
   </Worker>
   <Worker>
      <UniqueID>12354</UniqueID>
      <Date>2023-10-11-07:00</Date>
      <ExpenseAmount>998.00</ExpenseAmount>
      <BonusAmount>400.00</BonusAmount>
   </Worker>
   <Worker>
      <UniqueID>4567</UniqueID>
      <Date>2023-10-06-07:00</Date>
      <ExpenseAmount>1507.00</ExpenseAmount>
      <BonusAmount>322.00</BonusAmount>
   </Worker>
   <Worker>
      <UniqueID>4567</UniqueID>
      <Date>2023-10-10-07:00</Date>
      <ExpenseAmount>1507.00</ExpenseAmount>
      <BonusAmount>322.00</BonusAmount>
   </Worker>
</Workers>

问题

ExpenseAmount并且是不正确的BonusAmount

谁能帮我弄清楚我应该做什么来获得预期输出中提供的总和值?

XSLT 分组 XSLT-2.0

评论

0赞 michael.hor257k 11/2/2023
输入XML真的可以包含多个相同的元素吗?似乎您只需要将每个 .无论如何,您当前尝试的问题在于您的辅助分组再次对元素进行分组,而不是 .WorkerempIDCostWorkerWorkerCost

答:

1赞 michael.hor257k 11/2/2023 #1

我怀疑你只需要做:

XSLT 2.0 版

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

<xsl:template match="Workers">
    <Workers>
        <xsl:for-each select="Worker">
            <xsl:variable name="empID" select="empID" />
            <xsl:for-each-group select="Cost" group-by="Date">
                <Worker>
                    <UniqueID>
                        <xsl:value-of select="$empID"/>
                    </UniqueID>
                    <Date>
                        <xsl:value-of select="current-grouping-key()"/>
                    </Date>
                    <ExpenseAmount>
                        <xsl:value-of select="format-number(sum(current-group()[Type = 'Expense']/Amount), '0.00')"/>
                    </ExpenseAmount>
                    <BonusAmount>
                        <xsl:value-of select="format-number(sum(current-group()[Type = 'Bonus']/Amount), '0.00')"/>
                    </BonusAmount>
                </Worker>
            </xsl:for-each-group>
        </xsl:for-each>
    </Workers>
</xsl:template>

</xsl:stylesheet>

要按照您开始的方式执行此操作,需要:

<xsl:template match="Workers">
    <Workers>
        <xsl:for-each-group select="Worker" group-by="empID">
            <xsl:for-each-group select="current-group()/Cost" group-by="Date">
                <Worker>
                    <UniqueID>
                        <xsl:value-of select="../empID"/>
                    </UniqueID>
                    <Date>
                        <xsl:value-of select="current-grouping-key()"/>
                    </Date>
                    <ExpenseAmount>
                        <xsl:value-of select="format-number(sum(current-group()[Type = 'Expense']/Amount), '#.00')"/>
                    </ExpenseAmount>
                    <BonusAmount>
                        <xsl:value-of select="format-number(sum(current-group()[Type = 'Bonus']/Amount), '#.00')"/>
                    </BonusAmount>
                </Worker>
            </xsl:for-each-group>
        </xsl:for-each-group>
    </Workers>
</xsl:template>