tags:

views:

22

answers:

1

Hi!

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 :)

+2  A: 

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>
Oded
@Oded: Thanks! I had googled this, but hadn't come across data-type.
developer
@iHeartGreek - Always take a look at the specs or a tutorial site. They will have all the attributes and their meaning.
Oded