Help with xsl:sort

T

Tom Wright

Hi all

I'm using the following code with Apache and mod-xslt but it's not sorting
the elements as I want it to. At the moment, it sorts them with all items
starting with an uppercase letter first, like this:
A B C a b c

no matter what the setting of the case-order attribute is. It
rather looks like it's sorting them by ASCII (or unicode..?)
character order, rather than according to the language setting.

The lang attribute should be inferred automatically from the
system environment, but even if it is set manually (to "en-GB"),
the sort still does not work as expected.

According to the manual, with the attribute set to upper-first, it should do
this:
A a B b C c
....and with it set to lower-first, it should do this:
a A b B c C


Here's the code:

<xsl:apply-templates select="bookmark|alias[name(id(@ref)) = 'bookmark']"
mode="main">
<xsl:sort select="concat(title,id(@ref)/title)" lang="en-GB"/>
</xsl:apply-templates>

Thanks!
 
T

Tom Wright

Joe said:
Is there a reason you're trying to imply this using language, rather
than using the case-order attribute which is specifically intended for
the purpose?

Sorry, I should've explained better. I've tried doing it with just lang,
with just case-order and with both specified but none of it makes any
difference to the final sorting order. Is this a bug in mod-xslt?
 
T

Tom Wright

To clarify, I've tried using sort with each of its various options (as
listed on http://www.w3.org/TR/xslt#sorting), but no matter what I do it
always sorts the items like this:

A B C a b c

That's not a correct sorting order by any definition that I've seen.

I don't really mind whether it sorts upper case items before or after lower
case, but I would like it to sort all items beginning with an "A" of either
case before all items beginning with a "B" of either case. Either of the
following would be fine:

A a B b C c
a A b B c C

I hope that makes it a bit clearer - sorry, I didn't explain very well the
first time around.
 
T

Tom Wright

Joseph said:
Correct by ASCII (or Unicode) character order...

Yes ok, but that's not "culturally correct" as per the specs, and it's not
what any non-techie counts as a sensible sorting order.
 
J

Joseph Kesselman

Tom said:
Sorry, I should've explained better. I've tried doing it with just lang,
with just case-order and with both specified but none of it makes any
difference to the final sorting order. Is this a bug in mod-xslt?

Certainly sounds like it. Try other processors.
 
T

Tom Wright

Richard said:
Have you tried the same code with some other stylesheet processor?

Good plan - I didn't have any others on the machine, but have just installed
xalan and it gives the same output (mod-xslt is based on libxslt and xalan
is based on libxalan)

If anyone knows of another xslt processor which does sorting correctly, I'd
like to try it out.
 
P

p.lepin

Tom said:
Richard Tobin wrote:

[sorting problems - A a B b C c sort order wanted]
Good plan - I didn't have any others on the machine, but
have just installed xalan and it gives the same output
(mod-xslt is based on libxslt and xalan is based on
libxalan)

If anyone knows of another xslt processor which does
sorting correctly, I'd like to try it out.

How about using translate(), at least as a temporary
solution? It won't sort A and a, obviously, but at least
you'll get a A A a B b B C C c C etc., which is better than
nothing imho.
 
R

Richard Tobin

If anyone knows of another xslt processor which does sorting correctly, I'd
like to try it out.

I'm surprised Xalan doesn't, but you should try Saxon and even xt.

-- Richard
 
T

Tom Wright

Richard said:
I'm surprised Xalan doesn't, but you should try Saxon and even xt.

Saxon works! My distribution doesn't seem to include a packaged copy of xt,
so I'll skip that one. Thanks though, I've got something that works at
least. Not sure how to integrate it with Apache, but I'll investigate that
separately.
 
T

Tom Wright

How about using translate(), at least as a temporary
solution? It won't sort A and a, obviously, but at least
you'll get a A A a B b B C C c C etc., which is better than
nothing imho.

Yes, that works. Thanks, I was unaware of XSLT's translate() function. Now
I just have to decide whether it's better to install Saxon or use this
workaround. Thanks for your help, everyone.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top