xsl:sort with apply-templates 不排序 [英] xsl:sort with apply-templates not sorting
问题描述
我有一个相当大的 XSL 文档,用于完成许多事情的作业.它几乎完成了,但我错过了必须对其进行排序并且无法使其正常工作的要求.这是正在发生的事情的 SSCCE.
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" /> <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 非常缺乏经验.
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.
推荐答案
<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.
改为
<xsl:apply-templates select="staff/member">
<xsl:sort select="last_name" />
</xsl:apply-templates>
然后选择所有成员元素并对其进行排序.
then that selects all the member elements and sorts them.
这篇关于xsl:sort with apply-templates 不排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!