Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Sorting alphabetically & numerically

Reply
Thread Tools

Sorting alphabetically & numerically

 
 
A.T.
Guest
Posts: n/a
 
      07-26-2004
Am trying to sort the following file with the <xsl:sort> function, but
can't get it to work properly. Would like the id column sorted
according both alphabetically & numerically. (e.g. A-01, A-02, B-02).
Currently the output, is a table display.

XML DOCUMENT
<root>
<version>
<id>A-01</id>
<!-- other elements -removed for ease of reading-->
</version>
<version>
<id>B-02</id>
</version>
<version>
<id>A-02</id>
</version>
</root>

STYLESHEET
<xsl:template match="version">
<xsl:for-each select="version">
<xsl:sort select="id" data-type="text"
order="ascending"></xsl:sort>
</xsl:for-each>
</xsl:template>
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-27-2004


A.T. wrote:

> Am trying to sort the following file with the <xsl:sort> function, but
> can't get it to work properly. Would like the id column sorted
> according both alphabetically & numerically. (e.g. A-01, A-02, B-02).
> Currently the output, is a table display.
>
> XML DOCUMENT
> <root>
> <version>
> <id>A-01</id>
> <!-- other elements -removed for ease of reading-->
> </version>
> <version>
> <id>B-02</id>
> </version>
> <version>
> <id>A-02</id>
> </version>
> </root>


I think the following stylesheet does what you want:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="xml" encoding="UTF-8" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="version">
<xsl:sort data-type="text" order="ascending"
select="id" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

As long as you have the id format
x-dd
where x is a letter and d a digit the text comparison should yield the
correct result.

--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
A.T.
Guest
Posts: n/a
 
      07-27-2004
> As long as you have the id format
> x-dd
> where x is a letter and d a digit the text comparison should yield the >correct result.


How do you do the same sort with an id in the format x-dddd?
 
Reply With Quote
 
Richard Light
Guest
Posts: n/a
 
      07-29-2004
In message <(E-Mail Removed) >, A.T.
<(E-Mail Removed)> writes
>> As long as you have the id format
>> x-dd
>> where x is a letter and d a digit the text comparison should yield the
>>>correct result.

>
>How do you do the same sort with an id in the format x-dddd?


In the same way. If your numbers are left-padded with zeros, a text
sort will give you the expected result. Problems only arise where the
numbers within an element are _not_ left-padded.

If that were the case, you could split the element into its component
parts, and sort the numerical part as a number:

...
<xsl:apply-templates select="version">
<xsl:sort data-type="text" order="ascending"
select="substring-before(id, '-')" />
<xsl:sort data-type="number" order="ascending"
select="substring-after(id, '-')" />
</xsl:apply-templates>
...

(This only works with simple, consistent formats like the one you have
described: if you have an arbitrary mix of "text" and "number"
components in your sort strings you can't sort the numerical components
according to their integer value.)

Richard Light
--
Richard Light
SGML/XML and Museum Information Consultancy
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test a 'float' or 'double' zero numerically? Peng Yu C++ 14 09-15-2008 08:28 AM
Sorting a nested array (table) (alphabetically) nstott1982@gmail.com Perl Misc 26 10-22-2005 05:15 AM
Sorting a list alphabetically Only me Computer Support 3 11-02-2004 04:30 PM
program to numerically integrate rockwell C++ 7 09-25-2004 10:46 PM
Alphabetically sorting characters in a string Karsten Wutzke Java 3 01-07-2004 07:49 AM



Advertisments