Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

xslt - xsl:sort with apply-templates not sorting

I have quite a large XSL document for an assignment that does a number of things. It is nearly complete but I missed a requirement that it has to be sorted and I cannot get it working. Here is a SSCCE of what is happening.

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

The XML file looks like this

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

I was expecting the staff members to be listed by last name but they are not getting sorted. Please bear in mind that I am very inexperienced at XSLT.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    <xsl:apply-templates select="staff">
        <xsl:sort select="member/last_name" />
    </xsl:apply-templates>

selects the staff elements and sorts them, but there is only one staff element, so this is a no-op.

Change to

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

then that selects all the member elements and sorts them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...