XSLT的问题?sum,count

xml的结构如下,现在想计算总的价格,即120+210+3*15=85,如何用XSLT实现?

 1<a>
 2<num>1</num>
 3<price>20</price>
 4<a>
 5<a>
 6<num>2</num>
 7<price>10</price>
 8<a>
 9<a>
10<num>3</num>
11<price>15</price>
12<a>   
13  
14\---------------------------------------------------------------   
15  
16<?xml version="1.0" encoding="gb2312"?>
17<?xml-stylesheet type="text/xsl" href="test.xsl"?>
18<root>
19<a>
20<num>1</num>
21<price>20</price>
22</a>
23<a>
24<num>2</num>
25<price>10</price>
26</a>
27<a>
28<num>3</num>
29<price>15</price>
30</a>
31</root>
32<?xml version="1.0" encoding="gb2312"?>
33<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
34<xsl:template match="/root">
35<html>
36<head>
37</head>
38<body>
39<xsl:call-template name="t"></xsl:call-template>
40</body>
41</html>
42</xsl:template>
43<xsl:template name="t">
44<xsl:call-template name="mul">
45<xsl:with-param name="c" select="count(a)"></xsl:with-param>
46</xsl:call-template>
47</xsl:template>
48<xsl:template name="mul">
49<xsl:param name="c"></xsl:param>
50<xsl:choose>
51<xsl:when test="$c &gt; 1">
52<xsl:variable name="total">
53<xsl:call-template name="mul">
54<xsl:with-param name="c" select="$c - 1"></xsl:with-param>
55</xsl:call-template>
56</xsl:variable>
57<xsl:value-of select="$total+a[$c]/num*a[$c]/price"></xsl:value-of>
58</xsl:when>
59<xsl:when test="$c = 1">
60<xsl:value-of select="a[$c]/num*a[$c]/price"></xsl:value-of>
61</xsl:when>
62<xsl:otherwise></xsl:otherwise>
63</xsl:choose>
64</xsl:template>
65</xsl:stylesheet></a></a></a></a></a></a>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus