【发布时间】:2011-01-26 12:06:34
【问题描述】:
我正在使用 XSLT 对一段 XML 进行排序,例如:
<feed>
<entry>
<title>A To Z</title>
</entry>
<entry>
<title>Action</title>
</entry>
</feed>
XSLT 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="name" select="'title'" />
<xsl:param name="order" select="'ascending'" />
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="atom:feed">
<xsl:copy>
<xsl:apply-templates select="/atom:feed/*[not(self::atom:entry)]" />
<xsl:apply-templates select="/atom:feed/atom:entry">
<xsl:sort select="*[name() = $name]" order="{$order}" />
<xsl:sort select="atom:id" data-type="number" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我希望这些值按 A 到 Z,然后是 Action 的顺序出现,但结果却相反。看起来空格被忽略为排序依据。
【问题讨论】:
-
@smokedice 我删除了一些未使用的参数和名称空间,以使 XSLT 更易于阅读。如果您认为这些对问题很重要,请发表评论或编辑
-
@smokedice:好像对我有用,怎么失败了?
-
问题是生成的第一个标题是Action,然后是A到Z。@Lazarus你是用Xerces来应用模板的吗?
-
我使用了 Altova XMLSpy 中的内置引擎,对我来说效果很好。我认为@Nick Jones 可能已经在下面的回答中指出了这一点。
-
@smokedice:我不会使用
name(),除非我确定命名空间是默认命名空间还是绑定到前缀。另外,请注意您的输入文档没有 Atom 命名空间声明。