Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 具有分组和分组总数的XSLT_Xml_Xslt_Xpath - Fatal编程技术网

Xml 具有分组和分组总数的XSLT

Xml 具有分组和分组总数的XSLT,xml,xslt,xpath,Xml,Xslt,Xpath,我有以下XML数据。我无法控制这些数据的结构,这就是我接收数据的方式 <data> <row> <value name="CustomerID">1</value> <value name="CustomerName">Joe</value> <value name="Cost">22.50</value> </row> <row>

我有以下XML数据。我无法控制这些数据的结构,这就是我接收数据的方式

<data>
  <row>
    <value name="CustomerID">1</value>
    <value name="CustomerName">Joe</value>
    <value name="Cost">22.50</value>
  </row>
  <row>
    <value name="CustomerID">1</value>
    <value name="CustomerName">Joe</value>
    <value name="Cost">55.50</value>
  </row>
  <row>
    <value name="CustomerID">2</value>
    <value name="CustomerName">Jane</value>
    <value name="Cost">10</value>
  </row>
  <row>
    <value name="CustomerID">2</value>
    <value name="CustomerName">Jane</value>
    <value name="Cost">13.50</value>
  </row>
    <row>
    <value name="CustomerID">3</value>
    <value name="CustomerName">Jim</value>
    <value name="Cost">50</value>
  </row>
</data>

1.
乔
22.50
1.
乔
55.50
2.
简
10
2.
简
13.50
3.
吉姆
50
我需要使用XSLT v1.0来显示按客户ID分组的数据,以及每个客户的总数。我尝试搜索了许多文章,但没有一篇文章具有这种奇怪的结构。

使用:

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

    <xsl:key name="k" match="row" use="value[@name = 'CustomerID']"/>

    <xsl:template match="data">
        <xsl:copy>
            <xsl:apply-templates select="row[generate-id(.) = generate-id(key('k', value[@name = 'CustomerID']))]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="row">
        <xsl:copy>
            <xsl:copy-of select="value[@name = 'CustomerID']"/>
            <xsl:copy-of select="value[@name = 'CustomerName']"/>
            <sum>
                <xsl:value-of select="sum(key('k', value[@name = 'CustomerID'])/value[@name = 'Cost'])"/>
            </sum>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出:

<data>
  <row>
    <value name="CustomerID">1</value>
    <value name="CustomerName">Joe</value>
    <sum>78</sum>
  </row>
  <row>
    <value name="CustomerID">2</value>
    <value name="CustomerName">Jane</value>
    <sum>23.5</sum>
  </row>
  <row>
    <value name="CustomerID">3</value>
    <value name="CustomerName">Jim</value>
    <sum>50</sum>
  </row>
</data>

1.
乔
78
2.
简
23.5
3.
吉姆
50

V1.0更改了post Thankscellent,这正是我完成它所需要的。万分感谢!