我目前正在学习一些XSTL 2.0。我希望将XML文档转换为格式整齐的HTML文件。
我目前要做的是:
Group Suppliers, [sort them A-Z]
List their Items [sort them 1-n]
Calculate the QTY * Cost
Sum the cost of all orders from that supplier
Sum the total cost of all suppliers
输出应如下所示:
Supplier A
Item # Qty Item Price Total
1 5 5.50 27.50
3 10 5.50 55.00
Sub-total $82.50
Supplier B
Item # Qty Item Price Total
2 10 10 100.00
Sub-total $100.00
Total-cost $182.50
我可以让所有排序和分组等按预期工作,但我无法弄清楚如何计算小计或总成本值,因为它们是计算出来的,而不是原始XML文档的一部分。我是否有一些如何存储计算出的成本,以便在完成组,排序,计算功能后我可以将其汇总?
这是我目前的XML:
<?xml version="1.0"?>
<Orders>
<Order OrderNo="1">
<Supplier>Supplier A</Supplier>
<Quantity>5</Quantity>
<Price>5.50</Price>
</Order>
<Order OrderNo="2">
<Supplier>Supplier B</Supplier>
<Quantity>10</Quantity>
<Price>10</Price>
</Order>
<Order OrderNo="3">
<Supplier>Supplier A</Supplier>
<Quantity>10</Quantity>
<Price>5.50</Price>
</Order
这是当前的XSL 2.0文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title></title>
</head>
<body>
<p>
</p>
<xsl:for-each-group select="Orders/Order"
group-by="Supplier">
<xsl:sort select="Supplier" data-type="text" order="ascending" />
<xsl:for-each-group select="current-group()"
group-by="@OrderNo">
<xsl:sort select="@OrderNo" data-type="number" order="ascending" />
</xsl:for-each-group>
<table>
<tr>
<td colspan="4">
<xsl:value-of select="Supplier" />
</td>
</tr>
<tr>
<td>Item #</td>
<td>Qty</td>
<td>Item Price</td>
<td>Total</td>
</tr>
<xsl:for-each select="current-group()">
<xsl:sort select="@OrderNo" data-type="number" order="ascending" />
<tr>
<td>
<xsl:value-of select="@OrderNo" />
</td>
<td>
<xsl:value-of select="Quantity" />
</td>
<td>
<xsl:value-of select="Price" />
</td>
<td>
<xsl:value-of select="format-number(Quantity * Price,'0.00')" />
</td>
</tr>
</xsl:for-each>
<tr>
<td class="Sub-total" colspan="3">Sub-total</td>
<td></td>
</tr>
</table>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
以下是生成的HTML输出:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<p></p>
<table>
<tr>
<td colspan="4">Supplier A</td>
</tr>
<tr>
<td>Item #</td>
<td>Qty</td>
<td>Item Price</td>
<td>Total</td>
</tr>
<tr>
<td>1</td>
<td>5</td>
<td>5.50</td>
<td>27.50</td>
</tr>
<tr>
<td>3</td>
<td>10</td>
<td>5.50</td>
<td>55.00</td>
</tr>
<tr>
<td class="Sub-total" colspan="3">Sub-total</td>
<td></td>
</tr>
</table>
<table>
<tr>
<td colspan="4">Supplier B</td>
</tr>
<tr>
<td>Item #</td>
<td>Qty</td>
<td>Item Price</td>
<td>Total</td>
</tr>
<tr>
<td>2</td>
<td>10</td>
<td>10</td>
<td>100.00</td>
</tr>
<tr>
<td class="Sub-total" colspan="3">Sub-total</td>
<td></td>
</tr>
</table>
</body>
</html>
感谢您阅读冗长!只需要破解总和部分。
亲切的问候
本
答案 0 :(得分:2)
在XSLT 2.0中,您可以简单地执行:
<xsl:value-of select="format-number(sum(current-group()/(Quantity * Price)),'0.00')" />
获取当前Order
s。
答案 1 :(得分:1)
在当前的Order节点组上多次并应用sum:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title></title>
</head>
<body>
<p>
</p>
<xsl:for-each-group select="Orders/Order" group-by="Supplier">
<xsl:sort select="Supplier" data-type="text" order="ascending" />
<table>
<tr>
<td colspan="4">
<xsl:value-of select="Supplier" />
</td>
</tr>
<tr>
<td>Item #</td>
<td>Qty</td>
<td>Item Price</td>
<td>Total</td>
</tr>
<xsl:for-each select="current-group()">
<xsl:sort select="@OrderNo" data-type="number" order="ascending" />
<tr>
<td>
<xsl:value-of select="@OrderNo" />
</td>
<td>
<xsl:value-of select="Quantity" />
</td>
<td>
<xsl:value-of select="Price" />
</td>
<td>
<xsl:value-of select="format-number(Quantity*Price,'0.00')" />
</td>
</tr>
</xsl:for-each>
<tr>
<td class="Sub-total" colspan="3">Sub-total</td>
<td>
<xsl:value-of select="format-number(sum(current-group()/(Quantity*Price)),'0.00')"/>
</td>
</tr>
</table>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
</xsl:stylesheet>