我使用递归函数计算每张发票和每个客户的计算列(价格*数量)的总和。现在我需要计算每个客户的所有发票和所有客户的所有发票的总额。
xml看起来像这样:
<cinvoices>
<client> (with information @ client) </client>
<invoices>
<products>
<product> (information @ product: name, type ect and..
<price>123</price>
<quantity>21</quantity>
</product>
<product> (information @ product: name, type ect and..
<price>123</price>
<quantity>11</quantity>
</product>
</products>
<invoices>
<products>
<product> (information @ product: name, type ect and..
<price>32</price>
<quantity>3</quantity>
</product>
<product> (information @ product: name, type ect and..
<price>12</price>
<quantity>9</quantity>
</product>
</products>
</invoices>
</client>
<client>
<same as above>
</client>
</cinvoices>
xslt中使用的函数是:
<xsl:template name="sumProducts">
<xsl:param name="pList"/>
<xsl:param name="pRunningTotal" select="0"/>
<xsl:choose>
<xsl:when test="$pList">
<xsl:variable name="varMapPath" select="$pList[1]"/>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="$pList[position() > 1]"/>
<xsl:with-param name="pRunningTotal"
select="$pRunningTotal + $varMapPath/price * $varMapPath/quantity"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
$<xsl:value-of select="format-number($pRunningTotal, '#,##0.00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
===================== 函数调用如下:
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="*/*"/>
</xsl:call-template>
知道如何使用此功能计算每个客户的发票总额以及所有客户和所有发票的总计。
谢谢。答案 0 :(得分:1)
首先让我重新说一下你的要求。
以下模板:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="sumProducts">
<xsl:param name="pList"/>
<xsl:param name="pRunningTotal" select="0"/>
<xsl:choose>
<xsl:when test="$pList">
<xsl:variable name="varMapPath" select="$pList[1]"/>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="$pList[position() > 1]"/>
<xsl:with-param name="pRunningTotal" select="$pRunningTotal + $varMapPath/price * $varMapPath/quantity"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
$<xsl:value-of select="format-number($pRunningTotal, '#,##0.00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/cinvoices/client/invoices">
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="*/*"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文件:
<cinvoices>
<client>
<invoices>
<products>
<product>
<price>123</price>
<quantity>21</quantity>
</product>
<product>
<price>123</price>
<quantity>11</quantity>
</product>
</products>
</invoices>
<invoices>
<products>
<product>
<price>32</price>
<quantity>3</quantity>
</product>
<product>
<price>12</price>
<quantity>9</quantity>
</product>
</products>
</invoices>
</client>
<client>
<invoices>
<products>
<product>
<price>100</price>
<quantity>2</quantity>
</product>
<product>
<price>10</price>
<quantity>1</quantity>
</product>
</products>
</invoices>
</client>
</cinvoices>
应该产生以下输出:
<?xml version="1.0" encoding="UTF-8"?>
$3,936.00
$204.00
$210.00
所以这些是每张发票的价值。
您的解决方案使用XSLT命名模板。使用不同的技术可以解决这个问题,但我会坚持你已有的想法。
现在,使用以下代码修改最后一个模板:
<xsl:template match="/cinvoices/client">
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="*/*/*"/>
</xsl:call-template>
</xsl:template>
你会得到:
<?xml version="1.0" encoding="UTF-8"?>
$4,140.00
$210.00
这些是每个客户的所有发票的总计。
现在,使用以下代码修改最后一个模板:
<xsl:template match="/cinvoices">
<xsl:call-template name="sumProducts">
<xsl:with-param name="pList" select="*/*/*/*"/>
</xsl:call-template>
</xsl:template>
您将获得所有客户的总数:
<?xml version="1.0" encoding="UTF-8"?>
$4,350.00