473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sorting alphabetically & numerically

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>
Jul 20 '05 #1
3 5527


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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output 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/

Jul 20 '05 #2
> 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?
Jul 20 '05 #3
In message <53**************************@posting.google.com >, A.T.
<di*********@yahoo.com> 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
ri*****@light.demon.co.uk

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: Saputra | last post by:
Does anyone know how to sort a data view numerically? By default, when you sort a field from a table in a database, it sorts it in alpha-numerical order. In MS Access, sort is by alpha-numeric,...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
0
by: Martin H. | last post by:
Hi, I've got this problem with an enumeration of a property of a UserControl. The following code shows the problem: Public Enum TestEnum As Integer Arthur = 0 Zachary = -1 End Enum
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
7
by: Totti | last post by:
Hi all, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one tex area or data file and show the result in the 2nd text...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.