开发者

How to use xsl:sort with foreign characters (i.e., 'æ', 'ø' and 'å')

I'm trying to sort a list by the name of each element. Example:

Title One
Another Title
Å Another Title
Ø Yet Another Title

The output of this using the original sort-function will result in something like this:

Another Title
Title One
Å Another Title
Ø Yet Another Title 

While the desired out开发者_如何学Goput is:

Another Title
Title One
Ø Yet Another Title
Å Another Title

For those not familiar with the special characters 'Æ', 'Ø' and 'Å', these are three letters that is the Norwegian extension of the alphabet. So the Norwegian alphabet ends like this "...STUVWXYZÆØÅ". And due to this extension, using a regular xsl:sort will for example put 'Å' before 'Ø'. It seems to me that xsl sorts elements based on their ASCII values, and for some reason the ASCII values are not ordered correctly (i.e., not in the same order as the alphabet) for 'Æ', 'Ø' and 'Å'.

I've found a somewhat solution to this, however it requires more code than I like and I have to use it in several places (in the same xsl-document), making it even more cumbersome with the extensive code. Here's the approach I'm using right now:

<xsl:sort select="translate(current-grouping-key(), 'abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ0123456789', '0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZæÆøØåÅ')" data-type="text" order="ascending" case-order="lower-first"/>

As you see, this is quite a handful. Is it either a simpler way to use this in several places (like specifying a mode or something), or another approach that is not this extensive.


Provided that your XSLT processor recognises it, you should simply be able to add

lang="no"

to the xsl:sort element. (I think "no" is the language code for Norwegian, correct me if I am wrong.)


If the long strings does not changes (I'd assume they don't), you could store them in separate variables, and just use those instead? I know it's not the prettiest of solutions, but it would shorten your sort-expression, and make changes to the "alphabet" easier to realize.

Something like this:

<xsl:variable name="alpha1" select="abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ0123456789"/>
<xsl:variable name="alpha2" select="0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZæÆøØåÅ"/>
...
<xsl:sort select="translate(current-grouping-key(), $alpha1, $alpha2)" data-type="text" order="ascending" case-order="lower-first"/>

And maybe find some better names for the variables :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜