[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Translating roman numerals into integers with XSLT

Subject: Re: Translating roman numerals into integers with XSLT 2.0
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 21 Dec 2004 17:37:28 GMT
xslt ends with
> 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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.