在for-each-group之后添加Trailer Sum Amount

时间:2018-04-14 12:12:36

标签: xslt grouping

我很难找到如何在每个组之后添加一个预告片行。预告行应该具有每个部门之下每个部门的总和。以下是我的意思的一个例子。

下面是xml:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/bsvc">
   <wd:Report_Entry>
      <wd:Department>FISHING</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>99999999</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>1500</wd:Transaction_Amount>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Department>Clothing</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>44444444</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>250</wd:Transaction_Amount>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Department>Clothing</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>44444444</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>250</wd:Transaction_Amount>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Department>Clothing</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>22222222</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>500</wd:Transaction_Amount>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Department>Clothing</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>22222222</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>1500</wd:Transaction_Amount>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Department>FISHING</wd:Department>
      <wd:Ledger_Account>
         <wd:Ledger_ID>99999999</wd:Ledger_ID>
      </wd:Ledger_Account>
      <wd:Transaction_Amount>300</wd:Transaction_Amount>
   </wd:Report_Entry>
</wd:Report_Data>

以下是我的XSL:

<?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" version="2.0" 
    xmlns:wd="urn:com.workday.report/bsvc">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="no" method="text"/>
    <xsl:template match="/wd:Report_Data">
        <detail>
            <xsl:variable name="GroupDepartment">
                <xsl:for-each-group select="wd:Report_Entry" group-by="wd:Department"></xsl:for-each-group>             
            </xsl:variable>
            <xsl:variable name="GroupDepartmentAmount">
                <xsl:value-of select="sum(wd:Report_Entry[$GroupDepartment]/wd:Transaction_Amount)"/>    
            </xsl:variable>
            <xsl:for-each-group select="wd:Report_Entry" group-by="concat(wd:Department, wd:Ledger_Account/wd:Ledger_ID)">
                <!-- Deptartment -->
                <xsl:value-of select="wd:Department"/><xsl:call-template name="insertDelimiter"/>  
                <!-- Ledger Account -->
                <xsl:value-of select="current-group()[1]/wd:Ledger_Account[1]/wd:Ledger_ID"/><xsl:call-template name="insertDelimiter"/> 
                <!-- Sum by Ledger Account and Department-->
                <xsl:value-of select="sum(current-group()/wd:Transaction_Amount)"/><xsl:call-template name="insertDelimiter"/>       
            <xsl:call-template name="insertNewLine"/>

                <!-- Now Sum but Department after each the last grouping of Department -->
                  <xsl:choose>
                      <xsl:when test="../$GroupDepartment[position() = last()]">
                          <!-- Dept ID -->
                          <xsl:value-of select="wd:Department"/><xsl:call-template name="insertDelimiter"/>      
                          <!-- Amount -->
                          <xsl:value-of select="$GroupDepartmentAmount"/>
                        <xsl:call-template name="insertNewLine"/>
                      </xsl:when>
                </xsl:choose>
            </xsl:for-each-group>
        </detail>
    </xsl:template>
    <xsl:template name="insertDelimiter">
        <xsl:text>|</xsl:text>
    </xsl:template>
    <xsl:template name="insertNewLine">
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>    
</xsl:stylesheet>

以下是我目前的输出:

    FISHING|99999999|1800|
    FISHING|4300
    Clothing|44444444|500|
    Clothing|4300
    Clothing|22222222|2000|
    Clothing|4300

下面是所需的输出“

    FISHING|99999999|1800|
    FISHING|1800
    Clothing|44444444|500|
    Clothing|22222222|2000|
    Clothing|2500

如果您有任何疑问,请与我们联系。任何帮助深表感谢! -Remo

1 个答案:

答案 0 :(得分:3)

我会嵌套两个分组:

  <xsl:template match="Report_Data">
      <xsl:for-each-group select="Report_Entry" group-by="Department">
          <xsl:variable name="dep" select="current-grouping-key()"/>
          <xsl:for-each-group select="current-group()" group-by="Ledger_Account/Ledger_ID">
              <xsl:value-of select="$dep, current-grouping-key(), sum(current-group()/Transaction_Amount)" separator="|"/>
              <xsl:text>&#10;</xsl:text>
          </xsl:for-each-group>
          <xsl:value-of select="$dep, sum(current-group()/Transaction_Amount)" separator="|"/>
          <xsl:text>&#10;</xsl:text>
      </xsl:for-each-group>
  </xsl:template>

https://xsltfiddle.liberty-development.net/bdxtpJ/1包含

的完整代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xpath-default-namespace="urn:com.workday.report/bsvc"
    version="3.0">


  <xsl:output method="text"/>

  <xsl:template match="Report_Data">
      <xsl:for-each-group select="Report_Entry" group-by="Department">
          <xsl:variable name="dep" select="current-grouping-key()"/>
          <xsl:for-each-group select="current-group()" group-by="Ledger_Account/Ledger_ID">
              <xsl:value-of select="$dep, current-grouping-key(), sum(current-group()/Transaction_Amount)" separator="|"/>
              <xsl:text>&#10;</xsl:text>
          </xsl:for-each-group>
          <xsl:value-of select="$dep, sum(current-group()/Transaction_Amount)" separator="|"/>
          <xsl:text>&#10;</xsl:text>
      </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>