0

基本上我想按级别对数据进行排序,但我遇到了错误。我是 xslt 的新手。我尝试了下面的代码,但没有工作。

这是我的 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XSLTEx1WT.xsl"?>
<Modules>
    <module code="CSE2041">
        <name>Web Technologies II</name>
        <credit>3</credit>
        <level>2</level>
    </module>
    <module code="CSE2031Y">
        <name>Object Oriented Software Development</name>
        <credit>6</credit>
        <level>2</level>
    </module>
    <module code="CSE1041">
        <name>Web Technologies I</name>
        <credit>3</credit>
        <level>1</level>
    </module>
</Modules>

这是我的 xsl 文件

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:variable name="deptCredit" select="99"/>
    <xsl:variable name="myBorder" select="1"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>XSLT Exercise 1</title></head>
            <body>
                <table border="{$myBorder}">
                    <thead><tr><th>Module Name</th><th>No. of Credits</th><th>Level</th></tr></thead>
                    <tbody>

                        <xsl:apply-templates>
                            **<xsl:sort select="level" data-type="number"/>**
                        </xsl:apply-templates>
                    <tr><td colspan="3">Departmental credits needed: <xsl:value-of select="$deptCredit"/></td></tr>
                    <tr><td colspan="3">Percentage cleared: <strong> <xsl:value-of select="format-number(sum(//credit/text()) div $deptCredit,'##.##%')"/></strong>
                    </td></tr>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="module">
        <xsl:apply-templates select="@code"/>
            <tr><xsl:apply-templates select="*"/></tr>
    </xsl:template>

    <xsl:template match="name">
        <xsl:call-template name="sjName"/>
    </xsl:template>

    <xsl:template match="@code">
        <tr style="background-color:silver;">
            <td colspan="3" style="text-align:center;">
                <xsl:value-of select="."/>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="credit">
        <td style="color:blue;"><strong><xsl:value-of select="text()"/></strong></td>
    </xsl:template>

    <xsl:template match="level">
        <td style="color:blue;"><strong><xsl:value-of select="text()"/></strong></td>
    </xsl:template>

    <xsl:template name="sjName">
            <xsl:choose>
                <xsl:when test="contains(../@code,'Y')">
                    <td style="color:orange;font-weight:bold;"><xsl:value-of select="."/></td>
                </xsl:when>
                <xsl:otherwise>
                    <td style="color:lime;font-weight:bold;"><xsl:value-of select="."/></td>
                </xsl:otherwise>
            </xsl:choose>               
    </xsl:template>

</xsl:stylesheet>

我已经尝试了上述但它不工作

4

1 回答 1

1

你只需要改变这个:

<xsl:apply-templates>
    <xsl:sort select="level" data-type="number"/>
</xsl:apply-templates>

对此:

<xsl:apply-templates select="Modules/module">
    <xsl:sort select="level" data-type="number"/>
</xsl:apply-templates>
于 2013-03-04T10:59:44.497 回答