2017-04-13 37 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