Get the average of elements values in xsl

自古美人都是妖i 提交于 2019-12-02 07:11:45

问题


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>

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]?

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.

Thanks in advanced!


回答1:


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'])"/>

and just change the cid value for the different courses.



来源:https://stackoverflow.com/questions/16222647/get-the-average-of-elements-values-in-xsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!