【问题标题】:Calculate Average using XSLT XPath plus ascending order使用 XSLT XPath 加升序计算平均值
【发布时间】:2015-03-18 19:23:27
【问题描述】:

我正在尝试使用 XPath 获得平均值,但我得到的是 NaN 作为输出。

这是我的 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>

现在是 XSL 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="university">
<html>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Average</th>
</tr>
<xsl:for-each select="student">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="sum(.//student/@note) div count(.//    student/@note)"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我遇到的另一个问题是当我尝试按表中的升序对姓氏进行排序时。它只是行不通。

我尝试添加这段代码但没有成功:

<xsl:apply-templates select="student" >
<xsl:sort select="substring-after(name,' ')" order="ascending"/>
</xsl:apply-templates>

你们能帮我解决这个问题吗? 非常感谢!

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    试试这个方法?

    XSLT 1.0

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

    【讨论】:

    • 非常感谢迈克尔!最后一件事,如果我想将平均值四舍五入到小数点后 1 位,我该怎么办?再次感谢!
    • 我尝试将格式数字轮次添加到计数(课程),但出现错误。
    • 您想要取整平均值,而不是计数(始终是整数)。使用:&lt;xsl:value-of select="format-number(sum(course/@note) div count(course), '0.#')"/&gt;.
    • 我刚刚尝试了您的代码,但平均结果为 NaN。
    • 我的错!非常感谢迈克尔!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多