使用xslt将Roman Numeral设置为整数值

时间:2017-05-02 07:41:05

标签: xslt-2.0

是否有任何函数可以将罗马数转换为整数,格式数字函数可以将整数转换为罗马数。因为我正在努力将罗马数字转换为整数。如果没有功能,我必须在模板中处理这个。

1 个答案:

答案 0 :(得分:0)

我通过编写自己的函数来处理这个问题,但是有一个问题是它没有验证输入值,例如(VII或IVII是相同的)但是输出是正确的。

<强> XSLT

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"           
    xmlns:number="http://dummy" version="2.0" >

    <xsl:output method="text"/>

    <xsl:template match="/">
        (<xsl:value-of select="number:RomanToInteger('MMXVII', 0)"/>)
    </xsl:template>

    <xsl:function name="number:RomanToInteger">
        <xsl:param name="romannumber"/>
        <xsl:param name="followingvalue"/>
        <xsl:choose>
            <xsl:when test="ends-with($romannumber,'CM')">
                <xsl:value-of select="900 + number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 900)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'M')">
                <xsl:value-of select="1000+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 1000)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'CD')">
                <xsl:value-of select="400+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 400)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'D')">
                <xsl:value-of select="500+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 500)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'XC')">
                <xsl:value-of select="90+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 90)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'C')">
                <xsl:value-of select="(if(100 ge number($followingvalue)) then 100 else -100)+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 100)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'XL')">
                <xsl:value-of select="40+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 40)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'L')">
                <xsl:value-of select="50+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 50)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'IX')">
                <xsl:value-of select="9+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 9)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'X')">
                <xsl:value-of select="(if(10 ge number($followingvalue)) then 10 else -10) + number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 10)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'IV')">
                <xsl:value-of select="4+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-2), 4)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'V')">
                <xsl:value-of select="5+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 5)"/>
            </xsl:when>
            <xsl:when test="ends-with($romannumber,'I')">
                <xsl:value-of select="(if(1 ge number($followingvalue)) then 1 else -1)+ number:RomanToInteger(substring($romannumber,1,string-length($romannumber)-1), 1)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="0"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

</xsl:stylesheet>