【问题标题】:Modifying a XSLT XPATH code for a different output为不同的输出修改 XSLT XPATH 代码
【发布时间】:2015-04-01 22:20:34
【问题描述】:

我需要修改我的 XSLT 代码以获得不同的输出结果。我需要输出一个包含单数、学生人数和平均数的表格。

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

这是输出的样子:

非常感谢您的帮助!

【问题讨论】:

  • INF9430课程只有一个人注册-为什么平均39.0?另外,您应该注意一些以前的问题。接受或以其他方式对herehere 给出的答案作出反应。

标签: html xml xslt xslt-1.0


【解决方案1】:

您的输入文档格式不正确,在 Stackoverflow 上发布问题时请小心。

识别在内容或属性之一方面唯一的元素的标准方法是使用。对此最好的解释仍在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 输出

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


此外,如果您负责设计此 XML 文档,请注意其中的某些名称要么难以理解(“单一”),要么在上下文中不合适(“注释”)。正确的术语是“签名”和“等级”,或“分数”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2015-07-21
    • 2018-01-29
    相关资源
    最近更新 更多