Xml xslt显示总和前5名

Xml xslt显示总和前5名,xml,xslt,xpath,Xml,Xslt,Xpath,长期读者,第一次在这里张贴海报。我通常能够从网站上的其他帖子中获得很多信息,但我找不到解决这个问题的方法。 使用xslt,我目前能够显示每个客户发票的小计,然后是这些发票的总数,方法是将另一个变量$grandtotal添加到我下面的xslt模板中,并在循环的每个迭代中向其添加$sum 我现在需要做的是找到总发票数最高的前五名 这是我的XML的缩写版本: <bits> <client type="Commercial"> <clientid>1&

长期读者,第一次在这里张贴海报。我通常能够从网站上的其他帖子中获得很多信息,但我找不到解决这个问题的方法。 使用xslt,我目前能够显示每个客户发票的小计,然后是这些发票的总数,方法是将另一个变量
$grandtotal
添加到我下面的xslt模板中,并在循环的每个迭代中向其添加
$sum

我现在需要做的是找到总发票数最高的前五名

这是我的XML的缩写版本:

<bits>
    <client type="Commercial">
    <clientid>1</clientid>
            <inv>
            <invno>1</invno>
                <product>
                    <productid>321</productid>
                    <productprice>99.00</productprice>
                    <totalqty>2</totalqty>
                </product>
                <product>
                    <productid>333</productid>
                    <productprice>299.00</productprice>
                    <totalqty>1</totalqty>
            </product>
        </inv>
            <inv>
            <invno>2</invno>
                <product>
                    <productid>321</productid>
                    <productprice>99.00</productprice>
                    <totalqty>2</totalqty>
                </product>
                <product>
                    <productid>333</productid>
                    <productprice>299.00</productprice>
                    <totalqty>2</totalqty>
            </product>
        </inv>
    </client>
    <client type="Government">
        <clientid>2</clientid>
        <inv>
            <invno>3</invno>
            <product>
                <productid>399</productid>
                <productprice>1469.00</productprice>
                <totalqty>1</totalqty>
                </product>
            <product>
                <productid>354</productid>
                <productprice>15.00</productprice>
                <totalqty>1</totalqty>
            </product>
                <product>
                <productid>311</productid>
                <productprice>58.00</productprice>
                <totalqty>1</totalqty>
            </product>
                <product>
                    <productid>341</productid>
                    <productprice>199.00</productprice>
                    <totalqty>1</totalqty>
                </product>
        </inv>
    </client>
</bits>

1.
1.
321
99
2.
333
299
1.
2.
321
99
2.
333
299
2.
2.
3.
399
1469
1.
354
15
1.
311
58
1.
341
199
1.
我已使用以下代码计算每个客户的发票总额:

<xsl:for-each select="//client">
    <xsl:call-template name="sum">
        <xsl:with-param name="nodes" select="inv/product"/>
    </xsl:call-template>
</xsl:for-each>

<xsl:template name="sum">
<xsl:param name="nodes" />
<xsl:param name="sum" select="0" />
<xsl:variable name="current" select="$nodes[1]" />
<xsl:if test="$current">
  <xsl:call-template name="sum">
    <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" />
    <xsl:with-param name="sum" select="$sum + $current/totalqty * $current/productprice" />
  </xsl:call-template>
</xsl:if>
<xsl:if test="not($current)">
  <xsl:value-of select="$sum" />
</xsl:if>

我想做的是重复使用此代码,同时显示5个最高的合计发票及其相应的
类型,例如:

前五名:

  • 客户ID:2,发票号:3,发票总额:1741美元,类型:政府
  • 客户ID:1,发票号:2,发票总额:$796,类型:商业
  • 客户ID:1,发票号:1,发票总额:$497,类型:商业
  • 在过去,我使用了for循环
    显示前5名,但这是在查看存储值。 我需要另一个解决方案


    我需要在这一点上的一些提示,因为我仍然是非常新的标记

    此XSLT 1.0样式表…

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exsl="http://exslt.org/common"
      exclude-result-prefixes="xsl exsl">
    <xsl:output method="html" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:template match="/*">
      <table>
        <th><td>Client id</td><td>Invoice no</td><td>Invoice total</td>
            <td>Type</td></th>
        <xsl:variable name="rows">
          <xsl:apply-templates select="client/inv" />
        </xsl:variable>
        <xsl:for-each select="exsl:node-set($rows)/tr">
          <xsl:sort select="td[4]" data-type="number" order="descending" />
          <xsl:variable name="rank" select="position()" />
          <xsl:copy-of select="self::node()[$rank &lt; 6]" />
        </xsl:for-each>
      </table>
    </xsl:template>
    
    <xsl:template match="inv">
      <tr>
        <td><xsl:value-of select="../clientid" /></td>
        <td><xsl:value-of select="invno" /></td>
        <xsl:variable name="gross-prices">
          <xsl:for-each select="product">
            <t><xsl:value-of select="productprice * totalqty" /></t> 
          </xsl:for-each>  
        </xsl:variable>  
        <td><xsl:value-of select="sum( exsl:node-set($gross-prices)/t)" /></td>
        <td><xsl:value-of select="../@type" /></td>
      </tr>
    </xsl:template>
    
    </xsl:stylesheet>
    
    <bits>
        <client type="Commercial">
        <clientid>1</clientid>
                <inv>
                <invno>1</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>1</totalqty>
                </product>
            </inv>
                <inv>
                <invno>2</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>2</totalqty>
                </product>
            </inv>
        </client>
        <client type="Government">
            <clientid>2</clientid>
            <inv>
                <invno>3</invno>
                <product>
                    <productid>399</productid>
                    <productprice>1469.00</productprice>
                    <totalqty>1</totalqty>
                    </product>
                <product>
                    <productid>354</productid>
                    <productprice>15.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                    <productid>311</productid>
                    <productprice>58.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                        <productid>341</productid>
                        <productprice>199.00</productprice>
                        <totalqty>1</totalqty>
                    </product>
            </inv>
        </client>
    </bits>
    
    <table>
      <th>
        <td>Client id</td>
        <td>Invoice no</td>
        <td>Invoice total</td>
        <td>Type</td>
      </th>
      <tr>
        <td>2</td>
        <td>3</td>
        <td>1741</td>
        <td>Government</td>
      </tr>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>796</td>
        <td>Commercial</td>
      </tr>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>497</td>
        <td>Commercial</td>
      </tr>
    </table>
    
    
    客户无噪音总计
    类型
    
    …应用于此输入时…

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exsl="http://exslt.org/common"
      exclude-result-prefixes="xsl exsl">
    <xsl:output method="html" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:template match="/*">
      <table>
        <th><td>Client id</td><td>Invoice no</td><td>Invoice total</td>
            <td>Type</td></th>
        <xsl:variable name="rows">
          <xsl:apply-templates select="client/inv" />
        </xsl:variable>
        <xsl:for-each select="exsl:node-set($rows)/tr">
          <xsl:sort select="td[4]" data-type="number" order="descending" />
          <xsl:variable name="rank" select="position()" />
          <xsl:copy-of select="self::node()[$rank &lt; 6]" />
        </xsl:for-each>
      </table>
    </xsl:template>
    
    <xsl:template match="inv">
      <tr>
        <td><xsl:value-of select="../clientid" /></td>
        <td><xsl:value-of select="invno" /></td>
        <xsl:variable name="gross-prices">
          <xsl:for-each select="product">
            <t><xsl:value-of select="productprice * totalqty" /></t> 
          </xsl:for-each>  
        </xsl:variable>  
        <td><xsl:value-of select="sum( exsl:node-set($gross-prices)/t)" /></td>
        <td><xsl:value-of select="../@type" /></td>
      </tr>
    </xsl:template>
    
    </xsl:stylesheet>
    
    <bits>
        <client type="Commercial">
        <clientid>1</clientid>
                <inv>
                <invno>1</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>1</totalqty>
                </product>
            </inv>
                <inv>
                <invno>2</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>2</totalqty>
                </product>
            </inv>
        </client>
        <client type="Government">
            <clientid>2</clientid>
            <inv>
                <invno>3</invno>
                <product>
                    <productid>399</productid>
                    <productprice>1469.00</productprice>
                    <totalqty>1</totalqty>
                    </product>
                <product>
                    <productid>354</productid>
                    <productprice>15.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                    <productid>311</productid>
                    <productprice>58.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                        <productid>341</productid>
                        <productprice>199.00</productprice>
                        <totalqty>1</totalqty>
                    </product>
            </inv>
        </client>
    </bits>
    
    <table>
      <th>
        <td>Client id</td>
        <td>Invoice no</td>
        <td>Invoice total</td>
        <td>Type</td>
      </th>
      <tr>
        <td>2</td>
        <td>3</td>
        <td>1741</td>
        <td>Government</td>
      </tr>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>796</td>
        <td>Commercial</td>
      </tr>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>497</td>
        <td>Commercial</td>
      </tr>
    </table>
    
    
    1.
    1.
    321
    99
    2.
    333
    299
    1.
    2.
    321
    99
    2.
    333
    299
    2.
    2.
    3.
    399
    1469
    1.
    354
    15
    1.
    311
    58
    1.
    341
    199
    1.
    
    …产生…

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exsl="http://exslt.org/common"
      exclude-result-prefixes="xsl exsl">
    <xsl:output method="html" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:template match="/*">
      <table>
        <th><td>Client id</td><td>Invoice no</td><td>Invoice total</td>
            <td>Type</td></th>
        <xsl:variable name="rows">
          <xsl:apply-templates select="client/inv" />
        </xsl:variable>
        <xsl:for-each select="exsl:node-set($rows)/tr">
          <xsl:sort select="td[4]" data-type="number" order="descending" />
          <xsl:variable name="rank" select="position()" />
          <xsl:copy-of select="self::node()[$rank &lt; 6]" />
        </xsl:for-each>
      </table>
    </xsl:template>
    
    <xsl:template match="inv">
      <tr>
        <td><xsl:value-of select="../clientid" /></td>
        <td><xsl:value-of select="invno" /></td>
        <xsl:variable name="gross-prices">
          <xsl:for-each select="product">
            <t><xsl:value-of select="productprice * totalqty" /></t> 
          </xsl:for-each>  
        </xsl:variable>  
        <td><xsl:value-of select="sum( exsl:node-set($gross-prices)/t)" /></td>
        <td><xsl:value-of select="../@type" /></td>
      </tr>
    </xsl:template>
    
    </xsl:stylesheet>
    
    <bits>
        <client type="Commercial">
        <clientid>1</clientid>
                <inv>
                <invno>1</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>1</totalqty>
                </product>
            </inv>
                <inv>
                <invno>2</invno>
                    <product>
                        <productid>321</productid>
                        <productprice>99.00</productprice>
                        <totalqty>2</totalqty>
                    </product>
                    <product>
                        <productid>333</productid>
                        <productprice>299.00</productprice>
                        <totalqty>2</totalqty>
                </product>
            </inv>
        </client>
        <client type="Government">
            <clientid>2</clientid>
            <inv>
                <invno>3</invno>
                <product>
                    <productid>399</productid>
                    <productprice>1469.00</productprice>
                    <totalqty>1</totalqty>
                    </product>
                <product>
                    <productid>354</productid>
                    <productprice>15.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                    <productid>311</productid>
                    <productprice>58.00</productprice>
                    <totalqty>1</totalqty>
                </product>
                    <product>
                        <productid>341</productid>
                        <productprice>199.00</productprice>
                        <totalqty>1</totalqty>
                    </product>
            </inv>
        </client>
    </bits>
    
    <table>
      <th>
        <td>Client id</td>
        <td>Invoice no</td>
        <td>Invoice total</td>
        <td>Type</td>
      </th>
      <tr>
        <td>2</td>
        <td>3</td>
        <td>1741</td>
        <td>Government</td>
      </tr>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>796</td>
        <td>Commercial</td>
      </tr>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>497</td>
        <td>Commercial</td>
      </tr>
    </table>
    
    
    客户端id
    发票号
    发票总额
    类型
    2.
    3.
    1741
    政府
    1.
    2.
    796
    商业的
    1.
    1.
    497
    商业的
    
    笔记
    只要我们有权访问node-set(),就不需要折叠或分治来计算和。我们可以简单地使用本机sum()函数。

    XSLT的版本是什么?XSLT 1.0或2??按存储值,是指变量吗?“查看存储值”有什么错?如果现有的解决方案运行良好,为什么还需要另一个解决方案呢?XSLT1.0当我说存储值时,我应该说存储在XML中的元素非常有用!“包含无函数”错误有点问题,但以某种方式修复了它。@Jwah您能单击答案框旁边的复选标记接受答案吗?顺便问一下,您使用的是什么XSLT处理器?