Altova Mailing List Archives


Re: roman numeral to integer in XSL?

From: "Dan" <danochs111@-------.--->
To: NULL
Date: 9/16/2004 12:58:00 PM
Thanks!
looks like it is working for all except the "9"s.  ix, xix, xxix
If I have time I will repost with my fix...
dan


"Mukul Gandhi" <MukulGandhi@d...> wrote in message
news:06544CAD-4E37-45FF-98E8-EFDC9017F4EB@m......
> Sorry, there was a bug in my previous XSL. Please use this one -
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:output method="text" />
>
> <xsl:template match="/">
>   <xsl:call-template name="fromRoman">
>     <xsl:with-param name="no" select="'xv'" />
>     <xsl:with-param name="x" select="0" />
>     <xsl:with-param name="i" select="1" />
>   </xsl:call-template>
> </xsl:template>
>
> <xsl:template name="fromRoman">
>   <xsl:param name="no" />
>   <xsl:param name="x" />
>   <xsl:param name="i" />
>
>   <xsl:if test="$i <= string-length($no)">
>     <xsl:variable name="chr" select="translate(substring($no, $i,
> 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
>     <xsl:variable name="next">
>       <xsl:if test="$i < string-length($no)">
>         <xsl:value-of select="translate(substring($no, $i + 1,
> 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
>       </xsl:if>
>     </xsl:variable>
>     <xsl:variable name="prev">
>       <xsl:if test="$i > 1">
>         <xsl:value-of select="translate(substring($no, $i - 1,
> 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
>       </xsl:if>
>     </xsl:variable>
>     <xsl:if test="$chr = 'i'">
>       <xsl:choose>
>         <xsl:when test="normalize-space($next) = 'v'">
>            <xsl:call-template name="fromRoman">
>         <xsl:with-param name="no" select="$no" />
>          <xsl:with-param name="x" select="$x + 4" />
>          <xsl:with-param name="i" select="$i + 1" />
>            </xsl:call-template>
>         </xsl:when>
>         <xsl:otherwise>
>            <xsl:if test="normalize-space($next) = 'x'">
>              <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>           <xsl:with-param name="x" select="$x + 9" />
>           <xsl:with-param name="i" select="$i + 1" />
>              </xsl:call-template>
>            </xsl:if>
>            <xsl:if test="normalize-space($next) != 'x'">
>         <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>           <xsl:with-param name="x" select="$x + 1" />
>           <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>            </xsl:if>
>         </xsl:otherwise>
>       </xsl:choose>
>     </xsl:if>
>     <xsl:if test="$chr = 'v'">
>       <xsl:if test="normalize-space($prev) = 'i'">
>         <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>           <xsl:with-param name="x" select="$x" />
>           <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>       </xsl:if>
>       <xsl:if test="normalize-space($prev) != 'i'">
>         <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>           <xsl:with-param name="x" select="$x + 5" />
>           <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>       </xsl:if>
>     </xsl:if>
>     <xsl:if test="$chr = 'x'">
>       <xsl:if test="normalize-space($prev) != 'i'">
>         <xsl:if test="normalize-space($next) = 'l'">
>           <xsl:call-template name="fromRoman">
>      <xsl:with-param name="no" select="$no" />
>      <xsl:with-param name="x" select="$x + 40" />
>      <xsl:with-param name="i" select="$i + 1" />
>           </xsl:call-template>
>         </xsl:if>
>         <xsl:if test="normalize-space($next) != 'l'">
>   <xsl:if test="normalize-space($next) = 'c'">
>      <xsl:call-template name="fromRoman">
>        <xsl:with-param name="no" select="$no" />
>        <xsl:with-param name="x" select="$x + 90" />
>        <xsl:with-param name="i" select="$i + 1" />
>              </xsl:call-template>
>   </xsl:if>
>   <xsl:if test="normalize-space($next) != 'c'">
>     <xsl:call-template name="fromRoman">
>       <xsl:with-param name="no" select="$no" />
>       <xsl:with-param name="x" select="$x + 10" />
>       <xsl:with-param name="i" select="$i + 1" />
>             </xsl:call-template>
>   </xsl:if>
>         </xsl:if>
>       </xsl:if>
>     </xsl:if>
>     <xsl:if test="$chr = 'l'">
>       <xsl:if test="normalize-space($prev) != 'x'">
>         <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>   <xsl:with-param name="x" select="$x + 50" />
>   <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>       </xsl:if>
>     </xsl:if>
>     <xsl:if test="$chr = 'c'">
>       <xsl:if test="normalize-space($prev) != 'x'">
>         <xsl:if test="normalize-space($next) = 'd'">
>           <xsl:call-template name="fromRoman">
>      <xsl:with-param name="no" select="$no" />
>      <xsl:with-param name="x" select="$x + 400" />
>      <xsl:with-param name="i" select="$i + 1" />
>           </xsl:call-template>
>         </xsl:if>
>         <xsl:if test="normalize-space($next) != 'd'">
>   <xsl:if test="normalize-space($next) = 'm'">
>             <xsl:call-template name="fromRoman">
>        <xsl:with-param name="no" select="$no" />
>        <xsl:with-param name="x" select="$x + 900" />
>        <xsl:with-param name="i" select="$i + 1" />
>             </xsl:call-template>
>           </xsl:if>
>           <xsl:if test="normalize-space($next) != 'm'">
>     <xsl:call-template name="fromRoman">
>        <xsl:with-param name="no" select="$no" />
>        <xsl:with-param name="x" select="$x + 100" />
>        <xsl:with-param name="i" select="$i + 1" />
>             </xsl:call-template>
>           </xsl:if>
>         </xsl:if>
>       </xsl:if>
>     </xsl:if>
>     <xsl:if test="$chr = 'd'">
>       <xsl:if test="normalize-space($prev) != 'c'">
>         <xsl:call-template name="fromRoman">
>   <xsl:with-param name="no" select="$no" />
>   <xsl:with-param name="x" select="$x + 500" />
>   <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>       </xsl:if>
>     </xsl:if>
>     <xsl:if test="$chr = 'm'">
>       <xsl:if test="normalize-space($prev) != 'c'">
>         <xsl:call-template name="fromRoman">
>           <xsl:with-param name="no" select="$no" />
>   <xsl:with-param name="x" select="$x + 1000" />
>   <xsl:with-param name="i" select="$i + 1" />
>         </xsl:call-template>
>       </xsl:if>
>     </xsl:if>
>   </xsl:if>
>   <xsl:if test="$i > string-length($no)">
>     <xsl:value-of select="$x" />
>   </xsl:if>
> </xsl:template>
>
> </xsl:stylesheet>
>
> Regards,
> Mukul
>
> "Mukul Gandhi" wrote:
>
> > Hi Dan,
> >   Please try this XSL -
> >
> > <?xml version="1.0" encoding="ISO-8859-1"?>
> > <xsl:stylesheet version="1.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> >
> > <xsl:output method="text" />
> >
> > <xsl:template match="/">
> >   <xsl:call-template name="fromRoman">
> >     <xsl:with-param name="no" select="'xv'" />
> >     <xsl:with-param name="x" select="0" />
> >     <xsl:with-param name="i" select="1" />
> >   </xsl:call-template>
> > </xsl:template>
> >
> > <xsl:template name="fromRoman">
> >   <xsl:param name="no" />
> >   <xsl:param name="x" />
> >   <xsl:param name="i" />
> >
> >   <xsl:if test="$i <= string-length($no)">
> >     <xsl:variable name="chr" select="translate(substring($no,$i,
> > 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
> >     <xsl:variable name="next">
> >       <xsl:if test="$i > string-length($no)">
> >         <xsl:value-of select="translate(substring($no, $i + 1,
> > 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
> >       </xsl:if>
> >     </xsl:variable>
> >     <xsl:variable name="prev">
> >       <xsl:if test="$i > 1">
> >         <xsl:value-of select="translate(substring($no, $i - 1,
> > 1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
> >       </xsl:if>
> >     </xsl:variable>
> >     <xsl:if test="$chr = 'i'">
> >       <xsl:choose>
> >         <xsl:when test="normalize-space($next) = 'v'">
> >            <xsl:call-template name="fromRoman">
> >       <xsl:with-param name="no" select="$no" />
> >       <xsl:with-param name="x" select="$x + 4" />
> >       <xsl:with-param name="i" select="$i + 1" />
> >            </xsl:call-template>
> >         </xsl:when>
> >         <xsl:otherwise>
> >           <xsl:if test="normalize-space($next) = 'x'">
> >             <xsl:call-template name="fromRoman">
> >       <xsl:with-param name="no" select="$no" />
> >       <xsl:with-param name="x" select="$x + 9" />
> >       <xsl:with-param name="i" select="$i + 1" />
> >             </xsl:call-template>
> >           </xsl:if>
> >           <xsl:if test="normalize-space($next) != 'x'">
> >      <xsl:call-template name="fromRoman">
> >        <xsl:with-param name="no" select="$no" />
> >        <xsl:with-param name="x" select="$x + 1" />
> >        <xsl:with-param name="i" select="$i + 1" />
> >      </xsl:call-template>
> >           </xsl:if>
> >         </xsl:otherwise>
> >       </xsl:choose>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'v'">
> >       <xsl:if test="normalize-space($prev) != 'i'">
> >         <xsl:call-template name="fromRoman">
> >           <xsl:with-param name="no" select="$no" />
> >           <xsl:with-param name="x" select="$x + 5" />
> >           <xsl:with-param name="i" select="$i + 1" />
> >         </xsl:call-template>
> >       </xsl:if>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'x'">
> >       <xsl:if test="normalize-space($prev) != 'i'">
> >         <xsl:if test="normalize-space($next) = 'l'">
> >           <xsl:call-template name="fromRoman">
> >      <xsl:with-param name="no" select="$no" />
> >      <xsl:with-param name="x" select="$x + 40" />
> >      <xsl:with-param name="i" select="$i + 1" />
> >           </xsl:call-template>
> >         </xsl:if>
> >         <xsl:if test="normalize-space($next) != 'l'">
> >   <xsl:if test="normalize-space($next) = 'c'">
> >      <xsl:call-template name="fromRoman">
> >        <xsl:with-param name="no" select="$no" />
> >        <xsl:with-param name="x" select="$x + 90" />
> >        <xsl:with-param name="i" select="$i + 1" />
> >              </xsl:call-template>
> >   </xsl:if>
> >   <xsl:if test="normalize-space($next) != 'c'">
> >     <xsl:call-template name="fromRoman">
> >       <xsl:with-param name="no" select="$no" />
> >       <xsl:with-param name="x" select="$x + 10" />
> >       <xsl:with-param name="i" select="$i + 1" />
> >             </xsl:call-template>
> >   </xsl:if>
> >         </xsl:if>
> >       </xsl:if>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'l'">
> >       <xsl:if test="normalize-space($prev) != 'x'">
> >         <xsl:call-template name="fromRoman">
> >           <xsl:with-param name="no" select="$no" />
> >   <xsl:with-param name="x" select="$x + 50" />
> >   <xsl:with-param name="i" select="$i + 1" />
> >         </xsl:call-template>
> >       </xsl:if>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'c'">
> >       <xsl:if test="normalize-space($prev) != 'x'">
> >         <xsl:if test="normalize-space($next) = 'd'">
> >           <xsl:call-template name="fromRoman">
> >      <xsl:with-param name="no" select="$no" />
> >      <xsl:with-param name="x" select="$x + 400" />
> >      <xsl:with-param name="i" select="$i + 1" />
> >           </xsl:call-template>
> >         </xsl:if>
> >         <xsl:if test="normalize-space($next) != 'd'">
> >   <xsl:if test="normalize-space($next) = 'm'">
> >             <xsl:call-template name="fromRoman">
> >        <xsl:with-param name="no" select="$no" />
> >        <xsl:with-param name="x" select="$x + 900" />
> >        <xsl:with-param name="i" select="$i + 1" />
> >             </xsl:call-template>
> >           </xsl:if>
> >           <xsl:if test="normalize-space($next) != 'm'">
> >     <xsl:call-template name="fromRoman">
> >        <xsl:with-param name="no" select="$no" />
> >        <xsl:with-param name="x" select="$x + 100" />
> >        <xsl:with-param name="i" select="$i + 1" />
> >             </xsl:call-template>
> >           </xsl:if>
> >         </xsl:if>
> >       </xsl:if>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'd'">
> >       <xsl:if test="normalize-space($prev) != 'c'">
> >         <xsl:call-template name="fromRoman">
> >   <xsl:with-param name="no" select="$no" />
> >   <xsl:with-param name="x" select="$x + 500" />
> >   <xsl:with-param name="i" select="$i + 1" />
> >         </xsl:call-template>
> >       </xsl:if>
> >     </xsl:if>
> >     <xsl:if test="$chr = 'm'">
> >       <xsl:if test="normalize-space($prev) != 'c'">
> >         <xsl:call-template name="fromRoman">
> >           <xsl:with-param name="no" select="$no" />
> >   <xsl:with-param name="x" select="$x + 1000" />
> >   <xsl:with-param name="i" select="$i + 1" />
> >         </xsl:call-template>
> >       </xsl:if>
> >     </xsl:if>
> >   </xsl:if>
> >   <xsl:if test="$i > string-length($no)">
> >     <xsl:value-of select="$x" />
> >   </xsl:if>
> > </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > Regards,
> > Mukul
> >
> >
> > "Dan" wrote:
> >
> > > Is there a way to get an integer from a roman numeral in XSL?
Essentially,
> > > I want to input "xv" into a template and return "15"
> > > I can use <xsl:number> to go from integer to roman numeral, but I
don't see
> > > how to do the reverse.
> > >
> > > any help is appreciated!
> > > thanks, dan
> > >
> > >
> > >


Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.