求xml中各学生成绩的和(xslt)
实现效果图:
下面是xml代码:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="chengjidan.xslt"?>
<score>
<student>
<number>001</number>
<hearing>17</hearing>
<collo>24</collo>
<reading>18</reading>
</student>
<student>
<number>002</number>
<hearing>29</hearing>
<collo>35</collo>
<reading>30</reading>
</student>
<student>
<number>003</number>
<hearing>13</hearing>
<collo>18</collo>
<reading>15</reading>
</student>
<student>
<number>004</number>
<hearing>19</hearing>
<collo>26</collo>
<reading>29</reading>
</student>
<student>
<number>005</number>
<hearing>30</hearing>
<collo>32</collo>
<reading>27</reading>
</student>
</score>
这里是我写的xslt代码,实现上图的效果:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<html>
<head>
<title>英语成绩单</title>
</head>
<body>
<h2 align="center">英语成绩单</h2>
<table border="2" align="center">
<tr align="center">
<td>学号</td>
<td>听力</td>
<td>口语</td>
<td>阅读</td>
<td>总分</td>
</tr>
<xsl:for-each select="score/student">
<tr align="center">
<td>
<xsl:value-of select="number"/>
</td>
<xsl:choose>
<xsl:when test="hearing < 18">
<td bgcolor="#ffff00">
<xsl:value-of select="hearing"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="hearing"/>
</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="collo < 20">
<td bgcolor="#ffff00">
<xsl:value-of select="collo"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="collo"/>
</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="reading < 20">
<td bgcolor="#ffff00">
<xsl:value-of select="reading"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="reading"/>
</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="hearing+collo+reading < 70">
<td bgcolor="#ff0000">
<xsl:value-of select="hearing+collo+reading"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="hearing+collo+reading"/>
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
<tr align="center">
<td>平均分</td>
<xsl:variable name ="hearings">
<xsl:for-each select="//student">
<hear>
<xsl:value-of select="number(./hearing)"/>
</hear>
</xsl:for-each>
</xsl:variable>
<xsl:variable name ="collos">
<xsl:for-each select="//student">
<coll>
<xsl:value-of select="number(./collo)"/>
</coll>
</xsl:for-each>
</xsl:variable>
<xsl:variable name ="readings">
<xsl:for-each select="//student">
<read>
<xsl:value-of select="number(./reading)"/>
</read>
</xsl:for-each>
</xsl:variable>
<td>
<total>
<xsl:value-of select="sum(msxsl:node-set($hearings)/hear) div 5"/>
</total>
</td>
<td>
<total>
<xsl:value-of select="sum(msxsl:node-set($collos)/coll) div 5"/>
</total>
</td>
<td>
<total>
<xsl:value-of select="sum(msxsl:node-set($readings)/read) div 5"/>
</total>
</td>
<td>
<xsl:value-of select="(sum(msxsl:node-set($hearings)/hear)+
sum(msxsl:node-set($collos)/coll)+
sum(msxsl:node-set($readings)/read)) div 5"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
问问这代码还可以怎么简化不?
[解决办法]
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:template match="/"> <html> <head> <title>英语成绩单</title> </head> <body> <h2 align="center">英语成绩单</h2> <table border="2" align="center"> <tr align="center"> <td>学号</td> <td>听力</td> <td>口语</td> <td>阅读</td> <td>总分</td> </tr> <xsl:apply-templates select="score/student"/> <xsl:variable name="student.count" select="count(score/student)"/> <tr> <td>平均分</td> <td><xsl:value-of select="sum(score/student/hearing) div $student.count"/></td> <td><xsl:value-of select="sum(score/student/collo) div $student.count"/></td> <td><xsl:value-of select="sum(score/student/reading) div $student.count"/></td> <td><xsl:value-of select="sum(//hearing|//collo|//reading) div $student.count"/></td> </tr> </table> </body> </html> </xsl:template> <xsl:template match="student"> <tr align="center"> <td><xsl:value-of select="number"/></td> <td><xsl:if test="hearing < 18"><xsl:attribute name="bgcolor">#ffff00</xsl:attribute></xsl:if><xsl:value-of select="hearing"/></td> <td><xsl:if test="collo < 20"><xsl:attribute name="bgcolor">#ffff00</xsl:attribute></xsl:if><xsl:value-of select="collo"/></td> <td><xsl:if test="reading < 20"><xsl:attribute name="bgcolor">#ffff00</xsl:attribute></xsl:if><xsl:value-of select="reading"/></td> <xsl:variable name="total" select="hearing+collo+reading"/> <td><xsl:if test="$total < 70"><xsl:attribute name="bgcolor">#ff0000</xsl:attribute></xsl:if><xsl:value-of select="$total"/></td> </tr> </xsl:template></xsl:stylesheet>