xsl:sort with apply-templates not sorting



我有一个相当大的XSL文档,用于一个做很多事情的赋值。它几乎完成了,但我错过了一个要求,即必须对其进行排序,我无法使其工作。以下是正在发生的事情的SSCCE。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--   Root Document    -->
<xsl:template match="/">
    <html>
    <body>
        <xsl:apply-templates select="staff">
            <xsl:sort select="member/last_name" />
        </xsl:apply-templates>
    </body>
    </html>
</xsl:template>
<xsl:template match="member">
    <xsl:value-of select="first_name" />&#160;<xsl:value-of select="last_name" /> <br/>
</xsl:template>
</xsl:stylesheet>

XML文件看起来像这个

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sort.xsl"?>
<staff>
    <member>
        <first_name>Joe</first_name>
        <last_name>Blogs</last_name>
    </member>
    <member>
        <first_name>John</first_name>
        <last_name>Smith</last_name>
    </member>
    <member>
        <first_name>Steven</first_name>
        <last_name>Adams</last_name>
    </member>
</staff>

我原以为工作人员会按姓氏列出,但他们没有得到排序。请记住,我在XSLT方面非常缺乏经验。

    <xsl:apply-templates select="staff">
        <xsl:sort select="member/last_name" />
    </xsl:apply-templates>

选择staff元素并对其进行排序,但只有一个staff元素,因此这是no-op。

更改为

    <xsl:apply-templates select="staff/member">
        <xsl:sort select="last_name" />
    </xsl:apply-templates>

然后选择所有成员元素并对它们进行排序。

缺少的是一个人员匹配模板,或者将匹配模板更改为像下面这样的成员:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--   Root Document    -->
<xsl:template match="/">
    <html>
    <body>
        <xsl:apply-templates select="staff/member">
            <xsl:sort select="last_name" />
        </xsl:apply-templates>
    </body>
    </html>
</xsl:template>
<xsl:template match="member">
    <xsl:value-of select="first_name" />&#160;<xsl:value-of select="last_name" /> <br/>
</xsl:template>
</xsl:stylesheet>

最新更新