使用XSL将小写数值转化为中文大写数值

在MSXML中使用 <xsl:number format="&#x58f9"/> 等特定的format,可以将小写数值转化为大写数值。但是当XSL解析器不支持这种转换时只能得到一个整数值。

使用下面的XSL,可以正确的将一个数值转换为大写,或者转换为人民币。

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
 <xsl:output encoding="gb2312" method="html"/>
 <!--人民币转大写 只处理两位小数,第三位小数将被四舍五入-->
 <!--
 版权所有 叶建生
yjs_lh@sohu.com yjs_lh@sina.com
 @version 1.0 2005.03.26 01:58 于重庆南坪
 -->
 <xsl:template name="RMB">
  <xsl:param name="value"/>
  <xsl:choose>
   <xsl:when test="contains($value,'.')">
    <xsl:if test="translate(substring-before($value,'.'),'0','') != ''">
    <xsl:call-template name="TOUPPERCHSTR">
     <xsl:with-param name="value" select="substring-before($value,'.')"/>
    </xsl:call-template>
    <xsl:text>元</xsl:text>
    </xsl:if>
    <xsl:variable name="d" select="round(substring(substring-after(concat($value,'000'), '.'), 1, 3) div 10)"/>
    <xsl:variable name="d1" select="floor($d div 10)"/>
    <xsl:variable name="d2" select="$d mod 10"/>
    <xsl:if test="$d1&gt;0">
     <xsl:call-template name="UPPERCHAR">
      <xsl:with-param name="value" select="$d1"/>
     </xsl:call-template>
     <xsl:text>角</xsl:text>
    </xsl:if>
    <xsl:if test="$d2 &gt; 0">
     <xsl:call-template name="UPPERCHAR">
      <xsl:with-param name="value" select="$d2"/>
     </xsl:call-template>
     <xsl:text>分</xsl:text>
    </xsl:if>
    <xsl:if test="not($d2) or $d2=0">整</xsl:if>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="TOUPPERCHSTR">
     <xsl:with-param name="value" select="$value"/>
    </xsl:call-template>
    <xsl:text>元整</xsl:text>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <!--  
  至少处理可 16 位整数而不丢失精度
 -->
 <!--转换数值为大写,传入的参数将被四舍五入取整-->
 <xsl:template name="TOUPPERCHSTR">
  <xsl:param name="value"/>
  <xsl:variable name="v" select="round($value)"/>
  <xsl:choose>
   <xsl:when test="$v &lt; 0">
    <xsl:text>负</xsl:text>
    <xsl:call-template name="TOUPPERCHSTR">
     <xsl:with-param name="value" select="$v * -1"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:when test="$v = 0">零</xsl:when>
   <xsl:when test="string-length($value) &gt; 8">
    <xsl:if test="translate(substring($value,1,string-length($value) - 8), '0','') != ''">
     <xsl:call-template name="TOUPPERCHSTR">
      <xsl:with-param name="value" select="substring($value,1,string-length($value) - 8)"/>
     </xsl:call-template>
     <xsl:text>亿</xsl:text>
    </xsl:if>
    <xsl:variable name="s" select="substring($value,string-length($value) - 7)"/>
    <xsl:if test="number($s) &gt; 0">
     <xsl:if test="translate(substring($value,1,string-length($value) - 8), '0','') != '' and substring($s,1,1) = '0'">零</xsl:if>
     <xsl:call-template name="TOUPPERCHSTR">
      <xsl:with-param name="value" select="$s"/>
     </xsl:call-template>
    </xsl:if>
   </xsl:when>
   <xsl:when test="string-length($v) &gt; 4">
    <xsl:call-template name="TOUPPERCHSTR">
     <xsl:with-param name="value" select="substring($v,1,string-length($v) - 4)"/>
    </xsl:call-template>
    <xsl:text>万</xsl:text>
    <xsl:variable name="s" select="substring($v,string-length($v) - 3)"/>
    <xsl:if test="number($s) &gt; 0">
     <xsl:if test="substring($s,1,1) = '0'">零</xsl:if>
     <xsl:call-template name="TOUPPERCHSTR">
      <xsl:with-param name="value" select="$s"/>
     </xsl:call-template>
    </xsl:if>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="UPPERCHAR">
     <xsl:with-param name="value" select="substring($v,1,1)"/>
    </xsl:call-template>
    <xsl:call-template name="getweightname">
     <xsl:with-param name="value" select="string-length($v)"/>
    </xsl:call-template>
    <xsl:variable name="v1" select="number(substring($v,2))"/>
    <xsl:if test="$v1 &gt;= 1">
     <xsl:if test="substring($v, 2, 1) = '0'">
      <xsl:choose>
       <xsl:when test="string-length($v) &gt; 5 and $v1 &lt; 10000">万</xsl:when>
       <xsl:otherwise>零</xsl:otherwise>
      </xsl:choose>
     </xsl:if>
     <xsl:call-template name="TOUPPERCHSTR">
      <xsl:with-param name="value" select="$v1"/>
     </xsl:call-template>
    </xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <!--将数值直接转为大写文字-->
 <xsl:template name="UPPERCHAR">
  <xsl:param name="value"/>
  <xsl:value-of select="translate($value,'0123456789','零壹贰叁肆伍陆柒捌玖')"/>
 </xsl:template>
 <!--获得位的中文位权名-->
 <xsl:template name="getweightname">
  <xsl:param name="value"/>
  <xsl:choose>
   <xsl:when test="$value mod 4 = 2">拾</xsl:when>
   <xsl:when test="$value mod 4 = 3">佰</xsl:when>
   <xsl:when test="$value mod 4 = 0">仟</xsl:when>
   <xsl:otherwise/>
  </xsl:choose>
 </xsl:template>
 
 <!--用于测试-->
 <xsl:template match="/">
  <html>
   <head>
    <META http-equiv="Content-Type" content="text/html;charset=gb2312"/>
    <title> 小写转大写函数示例 </title>
   </head>
   <body>
    <table border="1">
     <tbody>
      <tr>
       <th>原数值</th>
       <th>中文数值</th>
       <th>人民币大写</th>
      </tr>
      <xsl:for-each select="//i">
       <tr>
        <td>
         <xsl:value-of select="."/>
        </td>
        <td>
         <xsl:call-template name="TOUPPERCHSTR">
          <xsl:with-param name="value" select="."/>
         </xsl:call-template>
        </td>
        <td>
         <xsl:call-template name="RMB">
          <xsl:with-param name="value" select="."/>
         </xsl:call-template>
        </td>
       </tr>
      </xsl:for-each>
     </tbody>
    </table>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>


测试用的XML:

<?xml version="1.0" encoding="GB2312"?>
<Root>
 <i>1</i>
 <i>10</i>
 <i>100</i>
 <i>1000</i>
 <i>10000</i>
 <i>100000</i>
 <i>1000000</i>
 <i>10000000</i>
 <i>100000000</i>
 <i>1000000000</i>
 <i>10000000000</i>
 <i>100000000000</i>
 <i>1000000000000</i>
 <i>10000000000000</i>
 <i>100000000000000</i>
 <i>1000000000000000</i>
 <i>10000000000000000</i>
 <i>100000000000000000</i>
 <i>1000000000000000000</i>
 <i>10000000000000000000</i>
 <i>100000000000000000000</i>
 <i>0000000000000000000000</i>
 <i>00000000001000000000000</i>
 <i>000000000000000100000000</i>
 <i>00000000000000000100000000</i>
 <i>000000000000000000100000000</i>
 <i>0000000000000000000100000000</i>
 <i>00000000000000000000100000000</i>
 <i>000000000000000000000100000000</i>
 <i>00000000000000000000000000000001</i>
 <i>10000000000000000000001000000000</i>
 <i>1000000000000000000000100000000</i>
 <i>100000000000000000000010000000</i>
 <i>10000000000000000000001000000</i>
 <i>1000000000000000000000100000</i>
 <i>100000000000000000000010000</i>
 <i>10000000000000000000001000</i>
 <i>1000000000000000000000100</i>
 <i>100000000000000000000010</i>
 <i>00000200000000000000011</i>
 <i>0000000000000000000001</i>
 <i>000000000000000010000</i>
 <i>000000000000000000001</i>
 <i>10000100000000000001</i>
 <i>1000001000000000001</i>
 <i>100200000000000001</i>
 <i>10000000000000001</i>
 <i>1010000000000001</i>
 <i>100000001000001</i>
 <i>10001000000001</i>
 <i>1000000010001</i>
 <i>100101001010</i>
 <i>10000010001</i>
 <i>1000000001</i>
 <i>100000001</i>
 <i>10000001</i>
 <i>1000001</i>
 <i>100001</i>
 <i>10001</i>
 <i>1001</i>
 <i>101</i>
 <i>11</i>
 <i>9</i>
 <i>123456789</i>
 <i>-123456789</i>
 <i>12313.123</i>
 <i>12313.125</i>
 <i>12313.1236</i>
 <i>12313.001</i>
 <i>12313.005</i>
 <i>12313.501</i>
 <i>12313.606</i>
 <i>00.606</i>
 <i>0.006</i>
 <i>5.606</i>
 <i>10012301</i>
 <i>012010</i>
</Root>

输出结果:

原数值

中文数值

人民币大写

1

壹元整

10

壹拾

壹拾元整

100

壹佰

壹佰元整

1000

壹仟

壹仟元整

10000

壹万

壹万元整

100000

壹拾万

壹拾万元整

1000000

壹佰万

壹佰万元整

10000000

壹仟万

壹仟万元整

100000000

壹亿

壹亿元整

1000000000

壹拾亿

壹拾亿元整

10000000000

壹佰亿

壹佰亿元整

100000000000

壹仟亿

壹仟亿元整

1000000000000

壹万亿

壹万亿元整

10000000000000

壹拾万亿

壹拾万亿元整

100000000000000

壹佰万亿

壹佰万亿元整

1000000000000000

壹仟万亿

壹仟万亿元整

10000000000000000

壹亿亿

壹亿亿元整

100000000000000000

壹拾亿亿

壹拾亿亿元整

1000000000000000000

壹佰亿亿

壹佰亿亿元整

10000000000000000000

壹仟亿亿

壹仟亿亿元整

100000000000000000000

壹万亿亿

壹万亿亿元整

0000000000000000000000

零元整

00000000001000000000000

壹万亿

壹万亿元整

000000000000000100000000

壹亿

壹亿元整

00000000000000000100000000

壹亿

壹亿元整

000000000000000000100000000

壹亿

壹亿元整

0000000000000000000100000000

壹亿

壹亿元整

00000000000000000000100000000

壹亿

壹亿元整

000000000000000000000100000000

壹亿

壹亿元整

00000000000000000000000000000001

壹元整

10000000000000000000001000000000

壹仟万亿亿零壹拾亿

壹仟万亿亿零壹拾亿元整

1000000000000000000000100000000

壹佰万亿亿零壹亿

壹佰万亿亿零壹亿元整

100000000000000000000010000000

壹拾万亿亿亿壹仟万

壹拾万亿亿亿壹仟万元整

10000000000000000000001000000

壹万亿亿亿零壹佰万

壹万亿亿亿零壹佰万元整

1000000000000000000000100000

壹仟亿亿亿零壹拾万

壹仟亿亿亿零壹拾万元整

100000000000000000000010000

壹佰亿亿亿零壹万

壹佰亿亿亿零壹万元整

10000000000000000000001000

壹拾亿亿亿零壹仟

壹拾亿亿亿零壹仟元整

1000000000000000000000100

壹亿亿亿零壹佰

壹亿亿亿零壹佰元整

100000000000000000000010

壹仟万亿亿零壹拾

壹仟万亿亿零壹拾元整

00000200000000000000011

贰拾亿亿零壹拾壹

贰拾亿亿零壹拾壹元整

0000000000000000000001

壹元整

000000000000000010000

壹万

壹万元整

000000000000000000001

壹元整

10000100000000000001

壹仟亿零壹佰万亿零壹

壹仟亿零壹佰万亿零壹元整

1000001000000000001

壹佰亿零壹万亿零壹

壹佰亿零壹万亿零壹元整

100200000000000001

壹拾亿零贰佰万亿零壹

壹拾亿零贰佰万亿零壹元整

10000000000000001

壹亿亿零壹

壹亿亿零壹元整

1010000000000001

壹仟零壹拾万亿零壹

壹仟零壹拾万亿零壹元整

100000001000001

壹佰万亿零壹佰万零壹

壹佰万亿零壹佰万零壹元整

10001000000001

壹拾万零壹拾亿零壹

壹拾万零壹拾亿零壹元整

1000000010001

壹万亿零壹万零壹

壹万亿零壹万零壹元整

100101001010

壹仟零壹亿零壹佰万壹仟零壹拾

壹仟零壹亿零壹佰万壹仟零壹拾元整

10000010001

壹佰亿零壹万零壹

壹佰亿零壹万零壹元整

1000000001

壹拾亿零壹

壹拾亿零壹元整

100000001

壹亿零壹

壹亿零壹元整

10000001

壹仟万零壹

壹仟万零壹元整

1000001

壹佰万零壹

壹佰万零壹元整

100001

壹拾万零壹

壹拾万零壹元整

10001

壹万零壹

壹万零壹元整

1001

壹仟零壹

壹仟零壹元整

101

壹佰零壹

壹佰零壹元整

11

壹拾壹

壹拾壹元整

9

玖元整

123456789

壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖

壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整

-123456789

负壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖

负壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整

12313.123

壹亿贰仟叁佰壹拾叁

壹万贰仟叁佰壹拾叁元壹角贰分

12313.125

壹亿贰仟叁佰壹拾叁

壹万贰仟叁佰壹拾叁元壹角叁分

12313.1236

壹拾贰亿叁佰壹拾叁

壹万贰仟叁佰壹拾叁元壹角贰分

12313.001

壹亿贰仟叁佰壹拾叁

壹万贰仟叁佰壹拾叁元整

12313.005

壹亿贰仟叁佰壹拾叁

壹万贰仟叁佰壹拾叁元壹分

12313.501

壹亿贰仟叁佰壹拾肆

壹万贰仟叁佰壹拾叁元伍角整

12313.606

壹亿贰仟叁佰壹拾肆

壹万贰仟叁佰壹拾叁元陆角壹分

00.606

陆角壹分

0.006

壹分

5.606

伍元陆角壹分

10012301

壹仟零壹万贰仟叁佰零壹

壹仟零壹万贰仟叁佰零壹元整

012010

壹万贰仟零壹拾

壹万贰仟零壹拾元整

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值