2017-04-13 237 views
0

我正在学习一个xml类。我是初学者,我正在学习xslt 1.0。如何使用xslt来计算平均值并在表中显示结果

我在想如何计算每个学生的平均值,并将结果显示在正确的学生表中。目前,平均计算是不正确的,我不知道如何让平均值显示在名称旁边的另一列中。请保持您的答案简单,因为我是初学者。谢谢 !

结果应该是这样的:

Student  Average 
Jeff Cooper  70.0 
Laureen Hanley 95.0 
Peter Manning 74.3 
Robert Shaw  78.7 

这是我的xml文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<?xml-stylesheet href="temptransfo.xsl" type="text/xsl" ?> 
<university> 
<student><name>Robert Shaw</name> 
<course code="INF4830" note="90" /> 
<course code="INF1130" note="70" /> 
<course code="INF1330" note="76" /></student> 
<student><name>Peter Manning</name> 
<course code="INF4830" note="76" /> 
<course code="INF1130" note="73" /> 
<course code="INF1330" note="74" /></student> 
<student><name>Jeff Cooper</name> 
<course code="INF4930" note="40" /> 
<course code="INF1130" note="90" /> 
<course code="INF1330" note="80" /></student> 
<student><name>Laureen Hanley</name> 
<course code="INF4830" note="92" /> 
<course code="INF1330" note="98" /></student> 
</university> 

这是迄今为止我在我的XSL文件所做的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output 
    method="html" 
    encoding="UTF-8" 
    doctype-public="-//W3C//DTD HTML 4.01//EN" 
    doctype-system="http://www.w3.org/TR/html4/strict.dtd" 
    indent="yes" ></xsl:output> 

<xsl:template match="/"> 
<html> 
    <head> 
    <title>Exercice 1</title> 
</head> 
<body> 

    <table border ="1"> 
    <caption>Exercice 1</caption> 
    <tr> 
    <th>Student</th> 
    <th>Average</th> 
    </tr> 
    <xsl:apply-templates select="university/student" > 
    <xsl:sort select="substring-after(name,' ')" order="ascending"/> 
    </xsl:apply-templates> 

    </table> 

</body> 
</html>   
</xsl:template> 

    <xsl:template match="student"> 
    <tr> 
    <td> 
    <xsl:value-of select="name" /> 
    </td> 
    <td> 
    <xsl:value-of select="format-number((sum(preceding::course/@note) div count (preceding::course)),'##.0')"/> 
    </td> 
    </tr> 
    </xsl:template> 

</xsl:stylesheet> 

回答

2

恕我直言,你通过使用不正确缩进的XML输入来混淆你自己。否则,你会看到,course孩子student,平均可以计算简单地为:

<xsl:value-of select="format-number(sum(course/@note) div count(course),'#.0')"/> 
+0

而且我怎么做,使在普通显示正确的学生姓名旁边的另一列? – Zyplexx

+1

你已经拥有了所有的功能。只需将您的计算改为我的。 –

+0

明白了!你是对的 !我对缩进感到困惑!现在一切正常!非常感谢 ! :-) – Zyplexx