xml - xslt 显示前 5 个总和

标签 xml xslt xpath

长期读者,第一次发帖。我通常能够从网站上的其他帖子中获得相当多的信息,但我找不到这个特定问题的解决方案。 使用 xslt,我目前可以通过添加另一个变量 $grandtotal 显示每个客户发票的小计,然后显示这些发票的总计。到我下面的 xslt 模板并添加 $sum在循环的每次迭代中都指向它。

我现在需要做的是找到总计最高的前 5 个发票。

这是我的 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>

我使用以下代码来计算每个客户的发票总额:

<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 个最高求和发票及其相应的 <clientid>type例如:

前 5 名:

  1. 客户 ID:2,发票编号:3,发票总额:1741 美元,类型:政府
  2. 客户 ID:1,发票编号:2,发票总额:796 美元,类型:商业
  3. 客户 ID:1,发票编号:1,发票总额:497 美元,类型:商业

过去我使用过for循环<xsl:for-each select=...> <xsl:sort select="Total" data-type="number" order="descending"/> ... <xsl:if test="position()&lt;6"> 显示前 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>

注释

只要我们能够访问node-set(),我们就不需要折叠或分而治之来计算总和。我们可以简单地使用原生 sum() 函数。

关于xml - xslt 显示前 5 个总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13024217/

相关文章:

基于条件逻辑的 XSLT 过滤节点

java - 如何使用 java XPATH 处理空 XML 标签?

c# - 无法在 C# 中使用反射转换列表

xml - 将 XSLT 应用于来自 iOS 中的 Web 服务的 XML

xml - XSLT:执行流程的调用模板与模式

java - Apache 配置 - 处理同名的多个 XML 条目

java - Selenium - 如何在 Java 中按具有任何值的属性查找元素?

java - 如何使用xsl :variable in xsl:choose

python - 用 Python 加密 SOAP 信封

java - 在 java 中写入文件时加密 XML 元素