Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

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

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)

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  "

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
225 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

Result

*****  *****  *****  *****  *****  *****  *****  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    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...