我正在整理一份名单。但是当我对列表进行排序时,数字并没有按照我希望的方式排序。
以下是XSLT的相关部分:
<xsl:template match="index">
<index><xsl:for-each select="indexitem">
<xsl:sort select="translate(name, '<"', '')"/>
<xsl:sort select="searchscott" data-type="number"/>
<indexitem>
<xsl:apply-templates select="name"></xsl:apply-templates>
<xsl:apply-templates select="scott"/>
<xsl:apply-templates select="searchscott"/>
<xsl:apply-templates select="searchterm"/>
</indexitem></xsl:for-each></index>
</xsl:template>
示例XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<index xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><indexitem>
<name>3-Leaf Clover Snowflake</name>
<scott>(4102)</scott>
<searchscott>4102</searchscott>
<searchterm>3-Leaf Clover Snowflake</searchterm></indexitem>
<indexitem>
<name>2004 Olympic Games Athens</name>
<scott>(3863)</scott>
<searchscott>3863</searchscott>
<searchterm>2004 Olympic Games Athens</searchterm>
</indexitem>
<indexitem>
<name>1943, Turning The Tide</name>
<scott>(2765-)</scott>
<searchscott>2765-</searchscott>
<searchterm>1943: Turning The Tide</searchterm>
</indexitem>
<indexitem>
<name>50s Sporty Cars</name>
<scott>(3931-3935)</scott>
<searchscott>3931-3935</searchscott>
<searchterm>50s Sporty Cars</searchterm>
</indexitem>
<indexitem>
<name><Valley of the Yosemite>, by Albert Bierstadt</name>
<scott>(4346)</scott>
<searchscott>4346</searchscott>
<searchterm>Albert Bierstadt, Valley of the Yosemite</searchterm>
</indexitem>
<indexitem>
<name>13 - Star Flag over Independence Hall</name>
<scott>(1622)</scott>
<searchscott>1622</searchscott>
<searchterm>13 - Star Flag over Independence Hall</searchterm>
</indexitem>
<indexitem>
<name>1606 Voyage of Samuel de Champlain</name>
<scott>(4073)</scott>
<searchscott>4073</searchscott>
<searchterm>1606 Voyage of Samuel de Champlain</searchterm>
</indexitem>
<indexitem>
<name>Racing Car 1911</name>
<scott>(2262)</scott>
<searchscott>2262</searchscott>
<searchterm>17.Racing Car 1911</searchterm>
</indexitem>
<indexitem>
<name>Duryea, 1893</name>
<scott>(3019)</scott>
<searchscott>3019</searchscott>
<searchterm>1893 Duryea</searchterm>
</indexitem>
</index>
我是否在梦想我可以先将较小的数字排序?如:
<name>3-Leaf Clover Snowflake</name>
<name>13 - Star Flag over Independence Hall</name>
<name>50s Sporty Cars</name>
<name>1606 Voyage of Samuel de Champlain</name>
<name>2004 Olympic Games Athens</name>
或者,这是不可能的吗?
由于
答案 0 :(得分:0)
使用substring-before()
和translate()
尝试按照数字后面的空格对名称进行切片时,请考虑以下调整。这是我能做的最好的事情。尝试编辑xml的文档或软件输出,删除连字符(即3-Leaf
)并加入字母(例如50s
)。
另外,请注意处理器仍将其视为按字母顺序排序的字符串,而不是数值。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" />
<xsl:template match="index">
<index>
<xsl:for-each select="indexitem">
<xsl:sort select="substring-before(translate(name, '-', ' '), ' ')"/>
<xsl:copy-of select="." />
</xsl:for-each>
</index>
</xsl:template>
</xsl:stylesheet>