Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/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
Distinct xslt1.0中不同值的总和 1. 基础知识 20 2. GHK 30 1. XYZ 20_Distinct_Xslt 1.0 - Fatal编程技术网

Distinct xslt1.0中不同值的总和 1. 基础知识 20 2. GHK 30 1. XYZ 20

Distinct xslt1.0中不同值的总和 1. 基础知识 20 2. GHK 30 1. XYZ 20,distinct,xslt-1.0,Distinct,Xslt 1.0,我试图使用xslt1.0获取不同值的总和。 我希望像这样使用muenchian方法输出。每个账单将有多个产品。在一天结束时,我需要账单总数和总金额 <customer> <item> <BILLNO>1</BILLNO> <product>ABC</product> <AMT>20</AMT> </item> <item> <B

我试图使用xslt1.0获取不同值的总和。 我希望像这样使用muenchian方法输出。每个账单将有多个产品。在一天结束时,我需要账单总数和总金额

<customer>
  <item>
    <BILLNO>1</BILLNO>
    <product>ABC</product>
    <AMT>20</AMT>
  </item>
  <item>
    <BILLNO>2</BILLNO>
    <product>GHK</product>
    <AMT>30</AMT>
  </item>
  <item>
    <BILLNO>1</BILLNO>
    <product>XYZ</product>
    <AMT>20</AMT>
  </item>
</customer>

2.
50
谢谢你的帮助 ram

此Xslt样式表:

<sales>
  <totalbills>2</totalbills>
  <totalamount>50</totalamount>
</sales>

呈现以下输出:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="item-key" match="item" use="BILLNO/text()"/>

  <xsl:template match="/customer">
    <root>
      <xsl:for-each select="item[generate-id() = generate-id(key('item-key', BILLNO/text()))]">
        <sales>
          <totalbills>
            <xsl:value-of select="count(../item[BILLNO = current()/BILLNO])"/>
          </totalbills>
          <totalamount>
            <xsl:value-of select="sum(../item[BILLNO = current()/BILLNO]/AMT)"/>
          </totalamount>
        </sales>
      </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet>

2.
40
1.
30

这个简短而简单的转换(无
xsl:for each
,无
,无
text()
用法):

<?xml version="1.0" encoding="utf-8"?>
<root>
  <sales>
    <totalbills>2</totalbills>
    <totalamount>40</totalamount>
  </sales>
  <sales>
    <totalbills>1</totalbills>
    <totalamount>30</totalamount>
  </sales>
</root>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kBills" match="item" use="BILLNO"/>

 <xsl:variable name="vdistItems" select=
  "/*/*[generate-id() = generate-id(key('kBills', BILLNO)[1])]"/>

 <xsl:template match="/*">
     <sales>
      <totalbills><xsl:value-of select="count($vdistItems)"/></totalbills>
      <totalamount><xsl:value-of select="sum($vdistItems/AMT)"/></totalamount>
     </sales>
 </xsl:template>
</xsl:stylesheet>
<customer>
  <item>
    <BILLNO>1</BILLNO>
    <product>ABC</product>
    <AMT>20</AMT>
  </item>
  <item>
    <BILLNO>2</BILLNO>
    <product>GHK</product>
    <AMT>30</AMT>
  </item>
  <item>
    <BILLNO>1</BILLNO>
    <product>XYZ</product>
    <AMT>20</AMT>
  </item>
</customer>

应用于提供的XML文档时:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <sales>
    <totalbills>2</totalbills>
    <totalamount>40</totalamount>
  </sales>
  <sales>
    <totalbills>1</totalbills>
    <totalamount>30</totalamount>
  </sales>
</root>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kBills" match="item" use="BILLNO"/>

 <xsl:variable name="vdistItems" select=
  "/*/*[generate-id() = generate-id(key('kBills', BILLNO)[1])]"/>

 <xsl:template match="/*">
     <sales>
      <totalbills><xsl:value-of select="count($vdistItems)"/></totalbills>
      <totalamount><xsl:value-of select="sum($vdistItems/AMT)"/></totalamount>
     </sales>
 </xsl:template>
</xsl:stylesheet>
<customer>
  <item>
    <BILLNO>1</BILLNO>
    <product>ABC</product>
    <AMT>20</AMT>
  </item>
  <item>
    <BILLNO>2</BILLNO>
    <product>GHK</product>
    <AMT>30</AMT>
  </item>
  <item>
    <BILLNO>1</BILLNO>
    <product>XYZ</product>
    <AMT>20</AMT>
  </item>
</customer>

1.
基础知识

  • 该功能