p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Sorting by Roman Numerals (http://p2p.wrox.com/showthread.php?t=77669)

bonekrusher January 5th, 2010 09:36 AM

Sorting by Roman Numerals
 
Hi,

Is it possible to sort by roman numeral?

Suppose my xml is:
Code:

<part title="PAPERS" chapter="CHAPTER II/>
<part title="PAPERS" chapter="CHAPTER I/>
<part title="PAPERS" chapter="CHAPTER III/>
<part title="PAPERS" chapter="CHAPTER IV/>

Xslt Fragment:
Code:

<xsl:for-each-group select="part" group-by="@chapter">
            <xsl:sort select="current-grouping-key()" case-order="upper-first" order="ascending" data-type="text"/>

This will result as

Code:

CHAPTER III
CHAPTER II
CHAPTER I
CHAPTER IV

Desired Result:
Code:

CHAPTER I
CHAPTER II
CHAPTER III
CHAPTER IV

Thanks for the help.

mhkay January 5th, 2010 09:49 AM

In theory an implementation could provide a collation that understands roman numerals, but I think it's unlikely that any implementation does so.

I think you will have to write a function that translates roman numerals to numbers, and then sort on the resulting number. It shouldn't be too difficult.

samjudson January 5th, 2010 09:50 AM

I suspect you will have the only way to do this would be to provide your own custom collation and sorting algorymth using an extension script.

If you are using Saxon you could read this article, which gives some details of how custom collations are loaded by Saxon (see the Custom Compare at the end of the article).

http://www.ibm.com/developerworks/xm...x-xsltsorting/

bonekrusher January 5th, 2010 09:51 AM

Thanks Michael & Sam. That was plan "b".

Regards,

bonekrusher January 5th, 2010 10:22 AM

Sam,

Great link. I created a class to do the custom sorting for Saxon:

here is a copy if any needs one:

Code:

package xsltcollations;

import java.text.ParseException;
import java.text.RuleBasedCollator;

public class RomanNumeralCollation extends RuleBasedCollator {

    public RomanNumeralCollation() throws ParseException {
        super(romanNumerals);
    }

    private static String romanNumerals = ("< I < II < III < IV < V < VI< VII <" +
            "VIII < IX <X <XI <XII <XIII <XIV <XV <XVI <XVII <XVIII <XIX <XX" +
            " < XXI < XXII < XXIII < XXIV < XXV < XXVI < XXVII < XXVIII <XXIX <" +
            " XXX < XXXI< XL< L < LX< LXX < LXXX < XC < C < CI " +
            "< CL < CC < CCC < CD < D < DC <DCC <DCCC <CM <M < MDC < MDCC <MCM");
}

Thanks again.


All times are GMT -4. The time now is 09:52 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.