开发者

Needing XSLT sort to treat some strings as numbers

I am unsure if this is an error, or if this is just how XSLT sort works.

When I do the following:

<xsl:apply-templates select="//*[@id&lt;=50000]">
  <xsl:sort select="@id" />
</xsl:apply-templates>

The results are not being sorted as if they are numbers.

For example I would get the following results:

@id 0
@id 1
@id -1
@id 100
@id -100
@id 12345
@id 2
@id -2
开发者_开发知识库@id 200

etc..

But I would like results to be:

@id -100
@id -2
@id -1
@id 0
@id 1
@id 2
@id 100
@id 200
@id 12345

etc..

How can I get the sort to treat the results numerically?

I know number() can convert a string to a number, but I don't know how this would be used in this context.

Any suggestions of what I can do to fix this would be appreciated :)


The xsl:sort element will sort alphabetically by default.

You need to specify numeric sort order by adding the data-type attribute with a value of number:

<xsl:apply-templates select="//*[@id&lt;=50000]">
  <xsl:sort select="@id" data-type="number" />
</xsl:apply-templates>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜