为不同的输出修改 XSLT XPATH 代码

Modifying a XSLT XPATH code for a different output

我需要修改我的 XSLT 代码以获得不同的输出结果。我需要输出一个 table 以及学生人数和平均数。

这是我的 XML 代码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet href="class.xsl" type="text/xsl" ?>
<university>
<student><sname>Charlie Parker</name>
<course sigle="INF8430" note="69" />
<course sigle="INF1030" note="65" />
<course sigle="INF1230" note="73" /></student>
<student><name>Miles Davis</name>
<course sigle="INF8430" note="65" />
<course sigle="INF1030" note="77" />
<course sigle="INF1230" note="83" /></student>
<student><name>John Coltrane</name>
<course sigle="INF9430" note="24" />
<course sigle="INF1030" note="64" />
<course sigle="INF1230" note="56" /></student>
<student><name>Charles Mingus</name>
<course sigle="INF8430" note="34" />
<course sigle="INF1230" note="89" /></student>
</university>

到目前为止,这是我的 XSLT 代码:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/university">
<html>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Average</th>
</tr>
<xsl:for-each select="student">
<xsl:sort select="substring-after(name, ' ')"/>
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="sum(course/@note) div count(course)"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

输出应如下所示:

非常感谢您的帮助!

您输入的文档格式不正确,请在 Whosebug 上发帖时小心。

识别在内容或属性之一方面唯一的元素的标准方法是使用 。最好的解释还是在Jeni Tennison's web page.

如果平均列中的精度过高,您还可以考虑使用 round() 函数。

XSLT 样式表

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:key name="course-sigle" match="course" use="@sigle"/>

    <xsl:template match="/university">
        <html>
            <body>
            <table border="1">
                <tr>
                    <th>Sigle</th>
                    <th>Number of Students</th>
                    <th>Average</th>
                </tr>
                <xsl:for-each select="student/course[count(. | key('course-sigle', @sigle)[1]) = 1]">

                    <xsl:variable name="count" select="count(key('course-sigle', @sigle))"/>
                    <tr>
                        <td>
                            <xsl:value-of select="@sigle"/>
                        </td>
                        <td>
                            <xsl:value-of select="$count"/>
                        </td>
                        <td>
                            <xsl:value-of select="sum(key('course-sigle', @sigle)/@note) div $count"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

HTML输出

<html>
   <body>
      <table border="1">
         <tr>
            <th>Sigle</th>
            <th>Number of Students</th>
            <th>Average</th>
         </tr>
         <tr>
            <td>INF8430</td>
            <td>3</td>
            <td>56</td>
         </tr>
         <tr>
            <td>INF1030</td>
            <td>3</td>
            <td>68.66666666666667</td>
         </tr>
         <tr>
            <td>INF1230</td>
            <td>4</td>
            <td>75.25</td>
         </tr>
         <tr>
            <td>INF9430</td>
            <td>1</td>
            <td>24</td>
         </tr>
      </table>
   </body>
</html>

渲染HTML输出

包括圆函数,输出看起来像


此外,如果您负责设计此 XML 文档,请注意其中的某些名称要么难以理解 ("sigle"),要么不符合上下文 ("note").正确的术语是 "signature" 和 "grade",或 "score".