如何根据XSLT中的键值对hrs进行分组和求和

时间:2015-11-30 12:18:58

标签: xslt-2.0

有关如何根据以下内容获取结果的建议,请提供xslt代码 按周分组的xml值。预期的输出应为:

Year Week Tot Hrs  Ben Hrs
2015 33    80.7       0
2015 34    120.7      0

感谢您的帮助

XML:

    <?xml version="1.0" encoding="UTF-8"?>
   -<wd:Report_Data xmlns:wd="urn:com.workday.report/TLB024_TEST">
 -<wd:Report_Entry>
  -<wd:PAYROLL_RESULT_LINE>
   <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>33</wd:FISCAL_WEEK>
    <wd:TOT_HRS>40</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
 -<wd:PAYROLL_RESULT_LINE>
   <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>33</wd:FISCAL_WEEK>
    <wd:TOT_HRS>0.07</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
  -<wd:PAYROLL_RESULT_LINE>
   <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>34</wd:FISCAL_WEEK>
    <wd:TOT_HRS>40</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
   -<wd:PAYROLL_RESULT_LINE>
    <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>34</wd:FISCAL_WEEK>
    <wd:TOT_HRS>0.07</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
    </wd:Report_Entry>
 -<wd:Report_Entry>
   -<wd:PAYROLL_RESULT_LINE>
    <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>33</wd:FISCAL_WEEK>
    <wd:TOT_HRS>40</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
  -<wd:PAYROLL_RESULT_LINE>
    <wd:FISCAL_YEAR>2015</wd:FISCAL_YEAR>
    <wd:FISCAL_WEEK>34</wd:FISCAL_WEEK>
    <wd:TOT_HRS>40</wd:TOT_HRS>
    <wd:BEN_HRS>0</wd:BEN_HRS>
    </wd:PAYROLL_RESULT_LINE>
    </wd:Report_Entry>
  -<wd:Report_Entry>
</wd:Report_Data>

XSLT:

这是xslt代码,我试图无法获得所需的结果。

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:wd="urn:com.workday.report/TLB024_TEST" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:template match="/|*">
        <xsl:copy>
            <xsl:apply-templates select="*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="wd:Report_Entry">

    <File xmlns:xtt="urn:com.workday/xtt">
        <xsl:for-each select="wd:PAYROLL_RESULT_LINE">
            <Record>
                <xsl:variable name="Group" select="../wd:PAYROLL_RESULT_LINE[((wd:FISCAL_YEAR=current()/wd:FISCAL_YEAR) and (wd:FISCAL_WEEK=current()/wd:FISCAL_WEEK)]" />
                <xsl:if test="generate-id()=generate-id($Group[1])">
                <xsl:copy>
                    <fiscal_year xtt:align="left"><xsl:value-of select="wd:FISCAL_YEAR"/></fiscal_year>
                    <fiscal_week xtt:align="left"><xsl:value-of select="wd:FISCAL_WEEK"/></fiscal_week>
                    <tot_hrs xtt:fixedLength="7" xtt:align="left"><xsl:value-of select="format-number(sum($Group/wd:TOT_HRS), '0.##')"/></tot_hrs>
                    <ben_hrs xtt:fixedLength="7" xtt:align="left"><xsl:value-of select="format-number(sum($Group/wd:BEN_HRS), '0.##')"/></ben_hrs>
                    <Filler><xsl:text>&#xa;</xsl:text></Filler>
                </xsl:copy>
                </xsl:if>
             </Record>
        </xsl:for-each>
    </File>

    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果您使用的是XSLT 2.0,这是一个非常简单的分组问题:

<xsl:for-each-group select="wd:PAYROLL_RESULT_LINE"
  group-by="concat(wd:FISCAL_YEAR, '/', wd:FISCAL_WEEK)">
            <Record>
                    <fiscal_year xtt:align="left"><xsl:value-of select="wd:FISCAL_YEAR"/></fiscal_year>
                    <fiscal_week xtt:align="left"><xsl:value-of select="wd:FISCAL_WEEK"/></fiscal_week>
                    <tot_hrs xtt:fixedLength="7" xtt:align="left"><xsl:value-of select="format-number(sum(current-group()/wd:TOT_HRS), '0.##')"/></tot_hrs>
                    <ben_hrs xtt:fixedLength="7" xtt:align="left"><xsl:value-of select="format-number(sum(current-group()/wd:BEN_HRS), '0.##')"/></ben_hrs>
                    <Filler><xsl:text>&#xa;</xsl:text></Filler>
             </Record>
</xsl:for-each-group>