|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Translating roman numerals into integers with XSLT
Thanks David for pointing the bugs in my stylesheet.
Your XSL looks much better.. I always have something
to learn from your answers!
Regards,
Mukul
--- David Carlisle <davidc@xxxxxxxxx> wrote:
>
> > Following is an improved algorithm.. It will work
> for
> > any large number..
>
> I think it was safer with the upper limit. If you
> put in a string that
> is not a valid roman numeral, or not of the form
> generated by xsl:number
> you go into an infinte loop. So if you want numbers
> in "Clock" style so
> give "IIII" rather than "IV" you die.
>
> The following just interprets the string directly
> (from the end)
> not sure I have all (or enough) rules but it does
> the cases that it
> does...
>
> David
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
> xmlns:num="http://whatever" version="2.0" >
>
> <xsl:output method="text"/>
>
> <xsl:template match="/">
>
> [<xsl:value-of select="num:RomanToInteger('I')"/>]
> [<xsl:value-of select="num:RomanToInteger('II')"/>]
> [<xsl:value-of
> select="num:RomanToInteger('IIII')"/>]
> [<xsl:value-of select="num:RomanToInteger('IV')"/>]
> [<xsl:value-of
> select="num:RomanToInteger('MMCCX')"/>]
>
>
> </xsl:template>
>
>
>
>
> <xsl:function name="num:RomanToInteger"
> as="xs:integer">
> <xsl:param name="r" as="xs:string"/>
> <xsl:choose>
> <xsl:when test="ends-with($r,'XC')">
> <xsl:sequence select="90+
>
num:RomanToInteger(substring($r,1,string-length($r)-2))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'L')">
> <xsl:sequence select="50+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'C')">
> <xsl:sequence select="100+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'D')">
> <xsl:sequence select="500+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'M')">
> <xsl:sequence select="1000+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'IV')">
> <xsl:sequence select="4+
>
num:RomanToInteger(substring($r,1,string-length($r)-2))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'IX')">
> <xsl:sequence select="9+
>
num:RomanToInteger(substring($r,1,string-length($r)-2))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'IIX')">
> <xsl:sequence select="8+
>
num:RomanToInteger(substring($r,1,string-length($r)-2))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'I')">
> <xsl:sequence select="1+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'V')">
> <xsl:sequence select="5+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:when test="ends-with($r,'X')">
> <xsl:sequence select="10+
>
num:RomanToInteger(substring($r,1,string-length($r)-1))"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:sequence select="0"/>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:function>
>
>
>
> </xsl:stylesheet>
__________________________________
Do you Yahoo!?
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








