XML technologies / XSLT / XSLT Kochbuch / Numbers and calculations / Converting Roman numerals to numbers

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You need to convert a Roman numeral to a number.

Solution

Roman numerals do not use positional notation. Their value is obtained by adding or subtracting the fixed values of the symbols. If the next symbol has a lower or equal value, add; otherwise subtract:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.ora.com/XSLTCookbook/math">
  <ckbk:romans>
    <ckbk:roman value="1">i</ckbk:roman>
    <ckbk:roman value="1">I</ckbk:roman>
    <ckbk:roman value="5">v</ckbk:roman>
    <ckbk:roman value="5">V</ckbk:roman>
    <ckbk:roman value="10">x</ckbk:roman>
    <ckbk:roman value="10">X</ckbk:roman>
    <ckbk:roman value="50">l</ckbk:roman>
    <ckbk:roman value="50">L</ckbk:roman>
    <ckbk:roman value="100">c</ckbk:roman>
    <ckbk:roman value="100">C</ckbk:roman>
    <ckbk:roman value="500">d</ckbk:roman>
    <ckbk:roman value="500">D</ckbk:roman>
    <ckbk:roman value="1000">m</ckbk:roman>
    <ckbk:roman value="1000">M</ckbk:roman>
  </ckbk:romans>
  <xsl:variable name="ckbk:roman-nums" select="document('')/*/*/ckbk:roman"/>
  <xsl:template name="ckbk:roman-to-number">
    <xsl:param name="roman"/>  
    <xsl:variable name="valid-roman-chars">
      <xsl:value-of select="document('')/*/ckbk:romans"/>
    </xsl:variable>   
    <xsl:choose>
      <!-- wird wahr, wenn es Zeichen in dem String gibt, die keine römischen Zahlen sind -->
      <xsl:when test="translate($roman,$valid-roman-chars,'')">NaN</xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="ckbk:roman-to-number-impl">
          <xsl:with-param name="roman" select="$roman"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="ckbk:roman-to-number-impl">
    <xsl:param name="roman"/>
    <xsl:param name="value" select="0"/>
    <xsl:variable name="len" select="string-length($roman)"/>
    <xsl:choose>
      <xsl:when test="not($len)">
        <xsl:value-of select="$value"/>
      </xsl:when>
      <xsl:when test="$len = 1">
        <xsl:value-of select="$value + $ckbk:roman-nums[. = $roman]/@value"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="roman-num" select="$ckbk:roman-nums[. = substring($roman, 1, 1)]"/>
        <xsl:choose>
          <xsl:when test="$roman-num/following-sibling::ckbk:roman = substring($roman, 2, 1)">
            <xsl:call-template name="ckbk:roman-to-number-impl">
              <xsl:with-param name="roman" select="substring($roman,2,$len - 1)"/>
              <xsl:with-param name="value" select="$value - $roman-num/@value"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="ckbk:roman-to-number-impl">
              <xsl:with-param name="roman" select="substring($roman,2,$len - 1)"/>
              <xsl:with-param name="value" select="$value + $roman-num/@value"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Discussion

The element xsl:number provides a convenient way to convert numbers to Roman numerals, but the reverse conversion is left to you. The recursive template shown above is straightforward and resembles the one in Jeni Tennison’s book XSLT and XPath on the Edge (M&T Books, 2001).

There are two minor caveats, usually not a problem. First, the solution does not work with mixed-case Roman numerals (for example, IiI). Such unusual strings are unlikely in sensible data sources, but the code would neither reject them nor return the correct value. Adding case normalization would let you reject these inputs or process them correctly.

The second caveat is that there is no standardized Roman representation of numbers greater than 1000. Saxon and Xalan keep Ms together, but another processor might behave differently.

If you do not want to store Roman-numeral data in the stylesheet, the following XPath 1.0 construct decodes a Roman numeral:

<xsl:variable name="roman-value" select="($c = 'i' or $c = 'I') * 1 + ($c = 'v' or $c = 'V') * 5 + ($c = 'x' or $c = 'X') * 10 + ($c = 'l' or $c = 'L') * 50 + ($c = 'c' or $c = 'C') * 100 + ($c = 'd' or $c = 'D') * 500 + ($c = 'm' or $c = 'M') * 1000)"/>

In XSLT 2.0 you can use a nested if-then-else expression or a lookup within a sequence:

<xsl:variable name="roman-value" select="(1,5,10,50,100,500,1000) [index-of(('I', 'V', 'X', 'L', 'C', 'D', 'M'),upper-case($c))]"/>

  

 

 

 

back to top