XSLT:按多个项目排序
我有 XML 数据,例如:
<feed>
<entry>
<id>4</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title2</title>
</entry>
<entry>
<id>3</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>2</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>1</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title</title>
</entry>
</feed>
我需要结果如下:
<feed>
<entry>
<id>1</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title</title>
</entry>
<entry>
<id>2</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>3</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>4</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title2</title>
</entry>
</feed>
基本上,我需要 XSLT 对标题进行排序,然后对 ID 进行排序。我已经制作了一个 XSLT,但较短的时间最后出现(使用 Xerces):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<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="*" />
<xsl:for-each select="atom:entry">
<xsl:sort select="string-length(atom:title)" order="descending" />
<xsl:sort select="atom:title" data-type="text" order="ascending" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="atom:feed/atom:entry"/>
</xsl:stylesheet>
I have XML data such as:
<feed>
<entry>
<id>4</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title2</title>
</entry>
<entry>
<id>3</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>2</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>1</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title</title>
</entry>
</feed>
And I need the outcome to result like:
<feed>
<entry>
<id>1</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title</title>
</entry>
<entry>
<id>2</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>3</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title1</title>
</entry>
<entry>
<id>4</id>
<updated>2011-01-18T16:55:54Z</updated>
<title>title2</title>
</entry>
</feed>
Basically I need the XSLT to sort on the title, then the ID. I have made an XSLT but the shorter times come out last (using Xerces):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<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="*" />
<xsl:for-each select="atom:entry">
<xsl:sort select="string-length(atom:title)" order="descending" />
<xsl:sort select="atom:title" data-type="text" order="ascending" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="atom:feed/atom:entry"/>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的输入示例(实际上不是 Atom feed),此样式表:
输出:
注意:只要没有不同的时区,此日期时间格式就可以像字符串(默认)一样排序。
For your input sample (not actually an Atom feed), this stylesheet:
Output:
Note: This date time format can be ordered like string (default) as long as there is no different time zone.