我的xml/xsl文件如下:
1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
2<!--模版即显示的样式,作用同css文件-->
3<xsl:template match="/">
4<html>
5<head>
6<title></title>
7</head>
8<body>
9<table border="1">
10<tr>
11<th>车名</th>
12<th>经销商</th>
13<th>销价</th>
14<th>数量</th>
15</tr>
16<xsl:for-each select="Cars/car">
17<tr>
18<xsl:apply-templates></xsl:apply-templates>
19<!--利用JavaScript脚本语言计算总价-->
20<xsl:eval language="JavaScript">
21countTotalPrice(this);
22</xsl:eval>
23</tr>
24</xsl:for-each>
25<tr>
26<td>
27<xsl:eval language="JavaScript">
28getTotalPrice();
29</xsl:eval>
30</td>
31<td>
32<xsl:eval language="JavaScript">
33getTotalAmount();
34</xsl:eval>
35</td>
36<td>
37<xsl:eval language="JavaScript">
38getavgPrice();
39</xsl:eval>
40</td>
41</tr>
42</table>
43</body>
44</html>
45</xsl:template>
46<!--车名模版-->
47<!--test 为判断条件,只有结果为true时,标记内的内容才会被xsl接受并处理-->
48<xsl:template match="BrandName">
49<xsl:choose>
50<xsl:when test=".[@nation='中国']">
51<td style="color:green">国产<xsl:value-of></xsl:value-of></td>
52</xsl:when>
53<xsl:when test=".[@nation='日本']">
54<td style="color:green">日制<xsl:value-of></xsl:value-of></td>
55</xsl:when>
56<xsl:otherwise>
57<td style="color:green">美制<xsl:value-of></xsl:value-of></td>
58</xsl:otherwise>
59</xsl:choose>
60</xsl:template>
61<!--其他模版-->
62<xsl:template match="Dealership">
63<!--用xsl:element name="标记名称"来代替<td></td>\-->
64<xsl:element name="TD">
65<xsl:value-of></xsl:value-of>
66</xsl:element>
67</xsl:template>
68<xsl:template match="Price">
69<td><xsl:value-of></xsl:value-of></td>
70</xsl:template>
71<xsl:template match="Amount">
72<td>
73<!--添加属性,作用如同<td style="color:green"></td>\-->
74<xsl:attribute name="STYLE">color:green</xsl:attribute>
75<xsl:value-of></xsl:value-of></td>
76</xsl:template>
77<!--总价计算-->
78<xsl:script language="JavaScript">
79var Total=0;
80var TotalAmount=0;
81function countTotalPrice(item)
82{
83var price=item.selectSingleNode("Price").nodeTypedValue;
84var amount=item.selectSingleNode("Amount").nodeTypedValue;
85TotalAmount=TotalAmount+amount;
86Total=Total+price*amount;
87}
88
89function getTotalPrice()
90{
91return Total;
92}
93
94function getTotalAmount()
95{
96return TotalAmount;
97}
98
99
100</xsl:script>
101</xsl:stylesheet>
1<cars>
2<car>
3<brandname nation="中国">甲车</brandname>
4<dealership>A厂</dealership>
5<price>500</price>
6<amount>3</amount>
7</car>
8<car>
9<brandname nation="日本">乙车</brandname>
10<dealership>B厂</dealership>
11<price>300</price>
12<amount>5</amount>
13</car>
14<car>
15<brandname nation="中国">丙车</brandname>
16<dealership>C厂</dealership>
17<price>500</price>
18<amount>4</amount>
19</car>
20<car>
21<brandname nation="朝鲜">丁车</brandname>
22<dealership>D厂</dealership>
23<price>1000</price>
24<amount>2</amount>
25</car>
26<car>
27<brandname nation="日本">戊车</brandname>
28<dealership>B厂</dealership>
29<price>300</price>
30<amount>5</amount>
31</car>
32</cars>
显示结果总价是对的,但是总数量却错误,我的程序算出来的结果是字符相加成了字符串,不知道是什么原因??请教!
---------------------------------------------------------------
try:
转换数据类型
var price=item.selectSingleNode("Price").nodeTypedValue;
var amount=item.selectSingleNode("Amount").nodeTypedValue;
==>
var price = parseInt( item.selectSingleNode("Price").text );
var amount = parseInt( item.selectSingleNode("Amount").text );
:_)