XML数据计算

XSL文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
	<head><title>student scores list</title></head>
	<body><center>
	<table border="1" bgcolor="#e0e0e0" bordercolor="teal">
	<caption>学生成绩表</caption>
	<tr>
		<th>学号</th>
		<th>姓名</th>
		<th>英语</th>
		<th>计算机基础</th>
		<th>高等数学</th>
		<th>模拟电子技术</th>
		<th>总分</th>
	</tr>
	<xsl:for-each select="scores/score">
	<tr>	
		<xsl:if test="position() mod 2=1">
			<xsl:attribute name="bgcolor">yellow</xsl:attribute>
		 </xsl:if>
		<td align="center"><xsl:value-of select="num"/></td>
		<td align="center"><xsl:value-of select="name"/></td>
		<td align="center">
	<xsl:attribute name="bgcolor">
		<xsl:choose>
		<xsl:when test="english &lt;60">red</xsl:when>	
		<xsl:when test="english &lt;70">blue</xsl:when>
		<xsl:when test="english &lt;80">yellow</xsl:when>	
		<xsl:when test="english &lt;90">green</xsl:when>
		<xsl:otherwise>	black</xsl:otherwise>
		</xsl:choose>
	</xsl:attribute>	
	<xsl:value-of  select="english"/>
		</td>
		<td align="center"><xsl:value-of select="computer"/></td>
		<td align="center"><xsl:value-of  select="math"/></td>
		<td align="center"><xsl:value-of  select="e_analog"/></td>
		<td align="center">
		   <xsl:value-of  select="english+computer+math+e_analog"/>
		</td>
	</tr>
	
	</xsl:for-each>
	<tr>
		<td align="center" colspan="2">平均分</td>
		<td align="center">
		<xsl:value-of  select="sum(//english) div count(//english)"/></td>
		<td align="center">
		<xsl:value-of select="sum(//computer) div count(//computer)"/></td>
		<td align="center">
			<xsl:value-of  select="sum(//math) div count(//math)"/>
		</td>
	<td align="center">
		<xsl:value-of  select="round(sum(//e_analog) div count(//e_analog))"/>
	</td>
	</tr>
	
	</table>
	</center>
	</body>
</html>
</xsl:template>
</xsl:stylesheet>

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<?xml:stylesheet type="text/xsl" href="li6-17.xsl"?>
<scores>
	<score term="2">
		<num>20040112</num>
		<name>王陵</name>
		<english>85</english>
		<computer>77</computer>
		<math>82</math>
		<e_analog>58</e_analog>
	</score>
	<score term="2">
		<num>20040101</num>
		<name>刘雯</name>
		<english>80</english>
		<computer>70</computer>
		<math>75</math>
		<e_analog>70</e_analog>
	</score>
	<score term="4">
		<num>20031514</num>
		<name>张艳丽</name>
		<english>69</english>
		<computer>90</computer>
		<math>80</math>
		<e_analog>75</e_analog>
	</score>
</scores>

效果图:

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像