我需要一种方法来计算每个客户的发票总额,。(将三张发票相加,然后相应地显示总额),.已尝试总额(/*/(PriceUnit*Ordered)),.但它不起作用---[发票总额=价格单位*已订购],.所以将三张发票相加,然后显示结果,.这对我来说很难,所以请帮助
示例输入文档
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Nom.xslt"?>
<customers>
<customer>
<clientname>troy madison</clientname>
<invoices>
<invoiceDate>8/8/98</invoiceDate>
<product>
<PriceUnit>1000</PriceUnit>
<Ordered>2</Ordered>
</product>
<product>
<PriceUnit>5400</PriceUnit>
<Ordered>3</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>1/4/98</invoiceDate>
<product>
<PriceUnit>300</PriceUnit>
<Ordered>4</Ordered>
</product>
<product>
<PriceUnit>6000</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>03/5/99</invoiceDate>
<product>
<PriceUnit>549</PriceUnit>
<Ordered>1</Ordered>
</product>
<product>
<PriceUnit>320</PriceUnit>
<Ordered>2</Ordered>
</product>
</invoices>
</customer>
<customer>
<clientname>Morris</clientname>
<invoices>
<invoiceDate>1/1/00</invoiceDate>
<product>
<PriceUnit>59</PriceUnit>
<Ordered>3</Ordered>
</product>
<product>
<PriceUnit>55</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>11/1/01</invoiceDate>
<product>
<PriceUnit>10</PriceUnit>
<Ordered>2</Ordered>
</product>
<product>
<PriceUnit>54</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>03/2/01</invoiceDate>
<product>
<PriceUnit>30</PriceUnit>
<Ordered>1</Ordered>
</product>
<product>
<PriceUnit>299</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
</customer>
</customers>
预期输出
[OP在此处列出预期输出。]
到目前为止我尝试过的样式表
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="customers">
<html>
<head>
<h1>CUSTOMER REFERENCE</h1>
</head>
<body bgcolor="#DAF52C">
<table border="1">
<tr>
<th width="50">NAME</th>
<th width="50">INVOICE DATE</th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">INVOICE DATE</th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">INVOICE DATE</th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">PRODUCT UNIT</th>
<th width="50">ORDERED </th>
<th width="50">INVOICE TOTAL</th>
</tr>
<xsl:for-each select="customer">
<tr>
<td><xsl:value-of select="clientname"/></td>
<xsl:for-each select="invoices">
<td><xsl:value-of select="invoiceDate"/></td>
<xsl:for-each select="product">
<td><xsl:value-of select="PriceUnit"/></td>
<td><xsl:value-of select="Ordered"/></td>
</xsl:for-each>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这里是一个不需要任何扩展函数的纯XSLT1.0解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<html>
<table border="1">
<thead>
<tr>
<td>Name</td><td>Total</td>
</tr>
</thead>
<xsl:apply-templates/>
</table>
</html>
</xsl:template>
<xsl:template match="customer">
<tr>
<td><xsl:value-of select="clientname"/></td>
<td>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="invoices/product"/>
<xsl:with-param name="pName1" select="'PriceUnit'"/>
<xsl:with-param name="pName2" select="'Ordered'"/>
</xsl:call-template>
</td>
</tr>
</xsl:template>
<xsl:template name="sumProducts">
<xsl:param name="pNodes"/>
<xsl:param name="pName1"/>
<xsl:param name="pName2"/>
<xsl:param name="pAccum" select="0"/>
<xsl:choose>
<xsl:when test="not($pNodes)">
<xsl:value-of select="$pAccum"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="$pNodes[position() >1]"/>
<xsl:with-param name="pName1" select="$pName1"/>
<xsl:with-param name="pName2" select="$pName2"/>
<xsl:with-param name="pAccum" select=
"$pAccum + $pNodes[1]/*[name()=$pName1] * $pNodes[1]/*[name()=$pName2]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当此转换应用于所提供的XML文档时:
<customers>
<customer>
<clientname>troy madison</clientname>
<invoices>
<invoiceDate>8/8/98</invoiceDate>
<product>
<PriceUnit>1000</PriceUnit>
<Ordered>2</Ordered>
</product>
<product>
<PriceUnit>5400</PriceUnit>
<Ordered>3</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>1/4/98</invoiceDate>
<product>
<PriceUnit>300</PriceUnit>
<Ordered>4</Ordered>
</product>
<product>
<PriceUnit>6000</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>03/5/99</invoiceDate>
<product>
<PriceUnit>549</PriceUnit>
<Ordered>1</Ordered>
</product>
<product>
<PriceUnit>320</PriceUnit>
<Ordered>2</Ordered>
</product>
</invoices>
</customer>
<customer>
<clientname>Morris</clientname>
<invoices>
<invoiceDate>1/1/00</invoiceDate>
<product>
<PriceUnit>59</PriceUnit>
<Ordered>3</Ordered>
</product>
<product>
<PriceUnit>55</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>11/1/01</invoiceDate>
<product>
<PriceUnit>10</PriceUnit>
<Ordered>2</Ordered>
</product>
<product>
<PriceUnit>54</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
<invoices>
<invoiceDate>03/2/01</invoiceDate>
<product>
<PriceUnit>30</PriceUnit>
<Ordered>1</Ordered>
</product>
<product>
<PriceUnit>299</PriceUnit>
<Ordered>1</Ordered>
</product>
</invoices>
</customer>
</customers>
生成所需的正确结果:
<html>
<table border="1">
<thead>
<tr>
<td>Name</td>
<td>Total</td>
</tr>
</thead>
<tr>
<td>troy madison</td>
<td>26589</td>
</tr>
<tr>
<td>Morris</td>
<td>635</td>
</tr>
</table>
</html>
解释:
一个递归调用的命名模板(
sumProducts
),它将节点的节点集($pNodes
)和两个子元素($pName1
和$pName2
)的名称作为参数,这些节点的子元素的乘积应该求和。最后还有一个初始值为0的辅助参数$pAccum
,用于存储每次递归调用之间的累积结果。停止条件是当传递的
pNodes
节点集为空时。在这种情况下,我们输出累加和($pAccum
)。如果停止条件为false,我们用
$pNodes
调用自己,其中第一个节点被删除,并用新的值$pAccum
调用自己,我们已经向其添加了当前乘积。
注意:
sumProducts
模板非常通用,可以用于任何需要产品总和的情况。
因此,它应该放在自己的样式表文件中,当需要产品的总和时,其他样式表将导入。
这是相当于Dimitre对您第一次发布这个问题的回答的XSLT1.0。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:so="https://stackoverflow.com/questions/12338944"
exclude-result-prefixes="xsl exsl so">
<xsl:output method="html" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<html>
<table border="1">
<thead>
<tr>
<td>Name</td><td>Total</td>
</tr>
</thead>
<xsl:apply-templates/>
</table>
</html>
</xsl:template>
<xsl:template match="customer">
<tr>
<td><xsl:value-of select="clientname"/></td>
<td>
<xsl:variable name="sales">
<xsl:for-each select="invoices/product">
<so:sale><xsl:value-of select="PriceUnit * Ordered"/></so:sale>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exsl:node-set($sales)/so:sale)"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
在示例文档中,这将产生输出。。。
<html>
<table border="1">
<thead>
<tr>
<td>Name</td>
<td>Total</td>
</tr>
</thead>
<tr>
<td>troy madison</td>
<td>26589</td>
</tr>
<tr>
<td>Morris</td>
<td>635</td>
</tr>
</table>
</html>