我使用递归函数来计算每张发票和每个客户的计算列(价格*数量)的总和。现在,我需要计算每个客户的所有发票以及所有客户的所有发票的总数。
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>
知道如何使用此函数计算每个客户的发票总额以及所有客户和所有发票的总计。
谢谢。
让我先改写一下你的要求。
以下模板:
<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