Subject: Re: [xsl] Translating roman numerals into integers with XSLT 2.0 From: David Carlisle <davidc@xxxxxxxxx> Date: Tue, 21 Dec 2004 17:37:28 GMT |
> 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> ________________________________________________________________________ This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk ________________________________________________________________________
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Translating roman numeral, António Mota | Thread | Re: [xsl] Translating roman numeral, Mukul Gandhi |
Re: [xsl] Cannot view XML input usi, Mukul Gandhi | Date | Re: [xsl] nested output, David Carlisle |
Month |