执行“;分组依据”;在XSLT1.0中使用sum函数

执行“;分组依据”;在XSLT1.0中使用sum函数,xslt,xslt-grouping,Xslt,Xslt Grouping,我希望得到如下表所示的结果: **Costc 1000** Product code Quantity Total amount SALESCOST 1 120.04 LEASINGRENT 1 59.90 **Costc 2000** Product code Quantity Total amount SALESCOST 1 118.94

我希望得到如下表所示的结果:

**Costc 1000**          
    Product code    Quantity    Total amount

    SALESCOST       1   120.04
    LEASINGRENT     1   59.90

**Costc 2000**          
    Product code    Quantity    Total amount

    SALESCOST       1   118.94
    LEASINGCOST     1   513.34

**Costc 3000**          
    Product code    Quantity    Total amount

    LEASINGCOST     1   658.24
    LEASINGRENT     2   100.80
我的输入文件是这样的

<?xml version="1.0" encoding="iso-8859-1"?>
<Details>
    <Detail>
        <LineNo>1</LineNo>
    <Products>
        <ProductCode>SALESCOST</ProductCode>
        <ProductDescr>Sales amount asset:997000000000</ProductDescr>
        <Quantity>1.00</Quantity>
    </Products>
    <Costc>
        <Value>2000</Value>
    </Costc>
    <TotAmount>118.94</TotAmount>
</Detail>
<Detail>
    <LineNo>2</LineNo>
    <Products>
        <ProductCode>LEASINGCOST</ProductCode>
        <ProductDescr>Leasing cost asset:997000000003</ProductDescr>
    </Products>
    <Costc>
        <Value>2000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>513.34</TotAmount>
</Detail>
<Detail>
    <LineNo>3</LineNo>
    <Products>
        <ProductCode>SALESCOST</ProductCode>
        <ProductDescr>Sales amount asset:997000000001</ProductDescr>
    </Products>
    <Costc>
        <Value>1000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>120.04</TotAmount>
</Detail>
<Detail>
    <LineNo>4</LineNo>
    <Products>
        <ProductCode>LEASINGCOST</ProductCode>
        <ProductDescr>Leasing cost asset:997000000002</ProductDescr>
    </Products>
    <Costc>
        <Value>3000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>658.24</TotAmount>
</Detail>
<Detail>
    <LineNo>5</LineNo>
    <Products>
        <ProductCode>LEASINGRENT</ProductCode>
        <ProductDescr>Leasing interest asset:997000000001</ProductDescr>
    </Products>
    <Costc>
        <Value>3000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>48.90</TotAmount>
</Detail>
<Detail>
    <LineNo>6</LineNo>
    <Products>
        <ProductCode>LEASINGRENT</ProductCode>
        <ProductDescr>Leasing interest asset:997000000002</ProductDescr>
    </Products>
    <Costc>
        <Value>3000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>51.90</TotAmount>
</Detail>
<Detail>
    <LineNo>7</LineNo>
    <Products>
        <ProductCode>LEASINGRENT</ProductCode>
        <ProductDescr>Leasing interest asset:997000000002</ProductDescr>
    </Products>
    <Costc>
        <Value>1000</Value>
    </Costc>
    <Quantity>1.00</Quantity>
    <TotAmount>59.90</TotAmount>
</Detail>

1.
销售成本
资产销售额:99700000000
1
2000
118.94
2.
租赁成本
租赁成本资产:99700000003
2000
1
513.34
3.
销售成本
资产销售金额:99700000001
1000
1
120.04
4.
租赁成本
租赁成本资产:99700000002
3000
1
658.24
5.
租赁租金
租赁利息资产:99700000001
3000
1
48.90
6.
租赁租金
租赁利息资产:99700000002
3000
1
51.90
7.
租赁租金
租赁利息资产:99700000002
1000
1
59.90

如何做到这一点? 它的多级分组,还有求和和和计数函数,对我来说太复杂了。 非常感谢

这个怎么样

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each-group select="*/Detail" group-by="Costc/Value" >
            <xsl:sort select="current-grouping-key()" order="ascending"/>

            <xsl:value-of select="concat('** Costc ',current-grouping-key())"/>
            <xsl:text>&#xD;&#xA;  Product code    Quantity    Total amount&#xD;&#xA;</xsl:text>

            <xsl:for-each-group select="current-group()" group-by="Products/ProductCode" >
                <xsl:text>   </xsl:text>            
                <xsl:value-of select="current-grouping-key()"/>
                <xsl:text>   </xsl:text>
                <xsl:value-of select="number(sum(current-group()/Quantity, current-group()/Products/Quantity))" />              
                <xsl:text>   </xsl:text>
                                <xsl:value-of select="sum(current-group()/TotAmount)" />                
                <xsl:text>&#xD;&#xA;</xsl:text>             
            </xsl:for-each-group>       

            <xsl:text>&#xD;&#xA;</xsl:text>

        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

在XSLT1.0中,您需要使用一种称为Meunchian分组的技术。在这种情况下,您将首先按成本成本元素进行分组,然后按产品元素进行分组

这意味着您需要定义两个键。首先按成本成本成本对详细信息进行分组

<xsl:key name="costc" match="Detail" use="Costc/Value"/>

然后按成本成本和产品对详细信息进行分组

<xsl:key name="product" match="Detail" use="concat(Costc/Value, '|', Products/ProductCode)"/>

(请注意连接中管道字符的使用。重要的是,此字符不会出现在值或产品代码中)

然后,要按Costc元素分组,您可以执行以下操作:

<xsl:apply-templates 
   select="Detail[generate-id() = generate-id(key('costc', Costc/Value)[1])]">

这将为每个独特的Costc元素获得第一个详细信息。然后,对于每个这样的组,您将按照该组中的产品进行分组

<xsl:apply-templates 
   select="../Detail
      [Costc/Value = current()/Costc/Value]
      [generate-id() 
         = generate-id(key('product', concat(Costc/Value, '|', Products/ProductCode))[1])]"  />

因此,考虑到以下XSLT

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

   <xsl:key name="costc" match="Detail" use="Costc/Value"/>
   <xsl:key name="product" match="Detail" use="concat(Costc/Value, '|', Products/ProductCode)"/>

   <xsl:template match="/Details">
      <xsl:apply-templates select="Detail[generate-id() = generate-id(key('costc', Costc/Value)[1])]" mode="Cost">
         <xsl:sort select="Costc/Value" />
      </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="Detail" mode="Cost">
      <h1><xsl:value-of select="Costc/Value" /></h1>
      <table>
         <tr>
            <td>Product Code</td>
            <td>Quantity</td>
            <td>Total amount</td>
         </tr>
         <xsl:apply-templates select="../Detail[Costc/Value = current()/Costc/Value][generate-id() = generate-id(key('product', concat(Costc/Value, '|', Products/ProductCode))[1])]" mode="Product" />
      </table>
   </xsl:template>

   <xsl:template match="Detail" mode="Product">
         <tr>
            <td><xsl:value-of select="Products/ProductCode" /></td>
            <td><xsl:value-of select="sum(key('product', concat(Costc/Value, '|', Products/ProductCode))//Quantity)" /></td>
            <td><xsl:value-of select="sum(key('product', concat(Costc/Value, '|', Products/ProductCode))/TotAmount)" /></td>
         </tr>
   </xsl:template>
</xsl:stylesheet>

产品代码
量
总额
应用于示例XML时,将输出以下内容:

<h1>1000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>SALESCOST</td>
        <td>1</td>
        <td>120.04</td>
    </tr>
    <tr>
        <td>LEASINGRENT</td>
        <td>1</td>
        <td>59.9</td>
    </tr>
</table>
<h1>2000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>SALESCOST</td>
        <td>1</td>
        <td>118.94</td>
    </tr>
    <tr>
        <td>LEASINGCOST</td>
        <td>1</td>
        <td>513.34</td>
    </tr>
</table>
<h1>3000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>LEASINGCOST</td>
        <td>1</td>
        <td>658.24</td>
    </tr>
    <tr>
        <td>LEASINGRENT</td>
        <td>2</td>
        <td>100.8</td>
    </tr>
</table>
1000
产品代码
量
总额
销售成本
1.
120.04
租赁租金
1.
59.9
2000
产品代码
量
总额
销售成本
1.
118.94
租赁成本
1.
513.34
3000
产品代码
量
总额
租赁成本
1.
658.24
租赁租金
2.
100.8

如果数量确实可以设置为两个级别,那么必须调整Tim的XSLT 1.0解决方案(参见结果,其中2000/SALESCOST的数量为0),同时考虑到第二个级别(在产品中)。请注意,卢卡斯已经对两个层次的数量进行了求和。我已经调整了我的答案,以处理两个不同层次的数量。非常感谢您的快速回答。这是完美的工作。我根本没想到我会得到答案。现在我很快得到了一个很好的答案。因此,我非常高兴,感激之情溢于言表。请转达我的问候Sweden@user1200589:“感激之泪”--太激动人心了。。。因此,用户通常会接受最佳答案。我希望你眼中的泪水不会阻止你接受答案提示:点击答案旁边的复选标记。@DimitreNovatchev ahhh…过去是这样的:)感激,美好的答案,等等。也许有一天我们会回到那里@杰迪嘉:我们为什么要等?我们都是人,能够以自然的方式行动和表达感情。这就是将我们联系在一起的机制。
<h1>1000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>SALESCOST</td>
        <td>1</td>
        <td>120.04</td>
    </tr>
    <tr>
        <td>LEASINGRENT</td>
        <td>1</td>
        <td>59.9</td>
    </tr>
</table>
<h1>2000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>SALESCOST</td>
        <td>1</td>
        <td>118.94</td>
    </tr>
    <tr>
        <td>LEASINGCOST</td>
        <td>1</td>
        <td>513.34</td>
    </tr>
</table>
<h1>3000</h1>
<table>
    <tr>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Total amount</td>
    </tr>
    <tr>
        <td>LEASINGCOST</td>
        <td>1</td>
        <td>658.24</td>
    </tr>
    <tr>
        <td>LEASINGRENT</td>
        <td>2</td>
        <td>100.8</td>
    </tr>
</table>