获取xsl中元素值的平均值 [英] Get the average of elements values in xsl
问题描述
今天我第一次接触xsl.我拥有的XML文件如下所示:
I've been dealing with xsl for the first time today. The XML file I have looks like the following:
<student_course>
<students>
<student num="">
<name gender=""></name>
<course cid="1"></course>
<course cid="2"></course>
<course cid="3"></course>
<course cid="4"></course>
<comments></comments>
</student>
</students>
<courses>
<course cid="1"></course>
<course cid="2"></course>
<course cid="3"></course>
<course cid="4"></course>
</courses>
</student_course>
有10多名学生,我需要为每位学生打印出每门课程的所有成绩.我已经完成了使用for-each的工作.我需要做的是,在该列的底部,显示该课程(学生/课程)的平均成绩,我当时认为变量是一种选择,然后将其逐个递增并除以某种东西,但是那似乎不起作用,每次我尝试创建一个变量时,我只能在设置它后立即调用它,这一定是做错了.我如何获得student_courses/students/student/course[@cid]
下所有值的平均值?
There are 10+ students, and I need to print out all the grades for each course for each student. That I've done using for-each. What I need to do, is at the bottom of the column, show the average grade for that course (student/course) I was thinking a variable would be the way to go, and increment it in the for-each and divide it or something, but that does not seem to work, everytime I try to make a variable I can only call it immediately after I set it, I must be doing something wrong. How would I get the average for all values under student_courses/students/student/course[@cid]
?
目前,我让我的xsl显示一个具有4列11行的表格(标题为1行,学生成绩为10行),然后是第12行以具有平均值.
Currently I have my xsl display a table with 4 columns and 11 rows (1 row for the titles, and 10 for the student grades) and then a 12th row to have the averages.
感谢进阶!
推荐答案
我知道了.要获得平均值,最简单的方法是使用for-each以及sum()和count()函数.我所需要做的就是将其添加到我希望平均值显示的位置:
I figured it out. To get the average, the easiest way was to use a for-each and the sum() and count() functions. All I needed was to add this where ever I wanted the average to display:
<xsl:value-of select="sum(/student_course/students/student/course[@cid='1']) div count(/student_course/students/student/course[@cid='1'])"/>
,只需更改不同课程的cid值即可.
and just change the cid value for the different courses.
这篇关于获取xsl中元素值的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!