[英]XSLT: Sorting like SOLR
我正在使用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,但是结果却相反。 好像空白被忽略为要排序的值。
此样式表:
<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="*[not(self::atom:entry)]" />
<xsl:apply-templates select="atom:entry">
<xsl:sort select="*[local-name() = $name]" order="{$order}" />
<xsl:sort select="atom:id" data-type="number" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用此输入( 带有名称空间声明 ):
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<title>A To Z</title>
</entry>
<entry>
<title>Action</title>
</entry>
</feed>
输出:
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<title>A To Z</title>
</entry>
<entry>
<title>Action</title>
</entry>
</feed>
已通过MSXSL 3/4,Saxon,Altova,XQSharp测试。 注意 :仅Oracle 和Xalan以升序对“ A至Z”之前的“动作”进行排序。
在XSLT 1.0中,排序是由实现定义的,因此某些实现很可能会忽略排序空间。 您正在使用哪种实现?
我建议类似的东西:
<xsl:sort select="translate(*[name() = $name],' ','_')" order="{$order}" />
可能会解决您的问题(尽管再次取决于您使用的XSLT的实现方式是“ _”排序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.