xslt - 在 for-each-group 之后添加预告片总金额

标签 xslt grouping

我很难弄清楚如何在 for-each-group 之后添加预告片行。预告片行应在 for-each group 下面包含每个部门的总和。下面是我的意思的一个例子。

下面是 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

如果您有任何疑问,请告诉我。任何帮助深表感谢! -雷莫

最佳答案

我会嵌套这两个分组:

  <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>

关于xslt - 在 for-each-group 之后添加预告片总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49831306/

相关文章:

mysql - 如何根据一列的最大值从 MySQL 中选择行并将另外两列分组?

LINQ 条件组

xml - XSL+XPATH : Compare previous node attribute to current node attribute

xslt - 如何使用xslt计算输出行数?

XSLT 选择包含特定子字符串的所有节点

javascript - 按对象数组javascript中的多个键进行分组

c++ - 如何实现级联相关对象的排序比较器?

r - R中不同级别的李克特分组

xml - 使用 XSLT 删除额外的组级别

xml - 在xsl tokenize中,如何获取前面的文本值