使用 XSLT 将数字转换为英文词义 [英] Convert Number into english Word-Meaning using XSLT

查看:26
本文介绍了使用 XSLT 将数字转换为英文词义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XSLT 非常陌生,并且有一个奇怪的需求,我还没有找到任何解决方案.服务器端我们有 2.0 版的 XSLT.

I am very much new to XSLT and have a weird requirement for which I have not found any solution. Server side we have XSLT of version 2.0.

输入数字可以是从零 (0) 到最多 8 位数字的任何值.我需要按照特定格式将其转换为英文词义.

Input number can be from Zero (0) to any value up to 8-digit number. I need to convert this into its English Word-Meaning following specific format.

如果数字是 5 位数字(例如 - 28651),那么前三组将是 5-Astric (*****) 后跟 2-空格,所以总共是 7.

If number is of 5 digit (Example - 28651) then First three set will be of 5-Astric (*****) followed by 2-space, so total 7.

如果任何数字的英文单词包含少于 5 个字母(例如 6 SIX 则必须后跟 2+2=4 个空格才能使其等同于 5 个字母的英文单词)

If English word of any digit contains less than 5 letter (example 6 SIX then it must be followed by 2+2=4 spaces to make it equivalent to 5 letter English WORD)

对于数字的最后一位也必须跟上面的 2 个空格.

For the Last digit of the number must also followed by 2 spaces as above.

   INPUT       OUTPUT (Without Enclosing Double-Quote)

    9375 = "*****  *****  *****  *****  NINE   THREE  SEVEN  FIVE   "
    8623 = "*****  *****  *****  *****  EIGHT  SIX    TWO    THREE  "
       0 = "*****  *****  *****  *****  *****  *****  *****  ZERO   "
   28651 = "*****  *****  *****  TWO    EIGHT  SIX    FIVE   ONE    "

40378623 = "FOUR   ZERO   THREE  SEVEN  EIGHT  SIX    TWO    THREE  "

我无法执行以下任何使用 Java 很容易的操作.

I am not able to perform any below operation which is quite easy with Java.

1.) 将任何值分配给现有变量.2.) 取倒数(345 到 543)3.) 在任何数组或其他东西中分别提取每个数字.

1.) Assign any value to existing variable back. 2.) Taking the reverse of the number (345 to 543) 3.) Fetch out each digit separately in any array or something.

这就是我对其方法的看法.

This is what I was thinking for its approach.

推荐答案

这在 XSLT 2.0 中其实并不难做到.考虑以下示例:

This is actually not very difficult to do in XSLT 2.0. Consider the following example:

XML

<input>
    <entry>0</entry>
    <entry>12</entry>
    <entry>345</entry>
    <entry>6789</entry>
    <entry>24680</entry>
    <entry>135797</entry>
    <entry>1234567</entry>
    <entry>87654321</entry>
</input>

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:variable name="strings" select="('ZERO   ', 'ONE    ', 'TWO    ', 'THREE  ', 'FOUR   ', 'FIVE   ', 'SIX    ', 'SEVEN  ', 'EIGHT  ', 'NINE   ')" />

<xsl:template match="/input">
    <xsl:for-each select="entry">
        <!-- padding -->
        <xsl:for-each select="1 to 8 - string-length(.)">
            <xsl:text>*****  </xsl:text>
        </xsl:for-each>
        <!-- digits to strings   -->
        <xsl:for-each select="string-to-codepoints(.)">
            <xsl:variable name="i" select="codepoints-to-string(.) " /> 
            <xsl:value-of select="$strings[number($i) + 1]"/>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

结果

*****  *****  *****  *****  *****  *****  *****  ZERO   
*****  *****  *****  *****  *****  *****  ONE    TWO    
*****  *****  *****  *****  *****  THREE  FOUR   FIVE   
*****  *****  *****  *****  SIX    SEVEN  EIGHT  NINE   
*****  *****  *****  TWO    FOUR   SIX    EIGHT  ZERO   
*****  *****  ONE    THREE  FIVE   SEVEN  NINE   SEVEN  
*****  ONE    TWO    THREE  FOUR   FIVE   SIX    SEVEN  
EIGHT  SEVEN  SIX    FIVE   FOUR   THREE  TWO    ONE    

这篇关于使用 XSLT 将数字转换为英文词义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆