473,698 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="ascendin g"></xsl:sort>
</xsl:for-each>
</xsl:template>
Jul 20 '05 #1
3 5542


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:styleshe et
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="ascendin g"
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*********@ya hoo.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="ascendin g"
select="substri ng-before(id, '-')" />
<xsl:sort data-type="number" order="ascendin g"
select="substri ng-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.d emon.co.uk

Jul 20 '05 #4

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

Similar topics

7
3261
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)) CType(local4, Short) = CType(src, Short)
22
4151
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) { return -1; } else
8
661
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, that is, numbers sort from 1, 10 ,11, 1X, 2, 21, 2X, etc. I want VB.NET to sort a column in data view numerically, so it goes 1 - 9, 10 - 19, 20 - 29, etc..
9
2606
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. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
0
1080
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
7188
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
3
7338
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
4
2938
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 using it because there are many concepts involved in this program that I want to understand like: 1.) degree of efficiency of this program as compared to sort using hash-table based approach. 2.) Keeping the program well structured so...
7
4207
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 area, in other words, howerever the words are set, horizontally the one following the other or vertically, the one under the other. i want it to work, i tried some code but i am having problems it only work when the text entered is horizontal like...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.