Home > other >  How to convert number 1, 2,3 in word format like One, two, Three in xslt
How to convert number 1, 2,3 in word format like One, two, Three in xslt

Time:11-24

I have an in put xml file which contain 20<.entry> element. the value of entry should be convert into word like "Twenty" I want to convert till number 100.

Input:

<input>
    <entry>46</entry>
</input>

XSLT:

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

<xsl:variable name="numbertoword">
<number Num="1" Word="One "/>
<number Num="2" Word="Two "/>
<number Num="3" Word="Three "/>
<number Num="4" Word="Four "/>
<number Num="5" Word="Five "/>
<number Num="6" Word="Six "/>
<number Num="7" Word="Seven "/>
<number Num="8" Word="Eight "/>
<number Num="9" Word="Nine "/>
<number Num="11" Word="Eleven " />
<number Num="12" Word="Twelve " />
<number Num="13" Word="Thirteen " />
<number Num="14" Word="Fourteen " />
<number Num="15" Word="Fifteen " />
<number Num="16" Word="Sixteen " />
<number Num="17" Word="Seventeen " />
<number Num="18" Word="Eighteen " />
<number Num="19" Word="Nineteen " />
<number Num="20" Word="Twenty" />
<number Num="21" Word="Twenty one"/>
<number Num="22" Word="Twenty two"/>
<number Num="23" Word="Twenty three"/>
<number Num="24" Word="Twenty four"/>
<number Num="25" Word="Twenty five"/>
<number Num="26" Word="Twenty six"/>
<number Num="27" Word="Twenty seven"/>
<number Num="28" Word="Twenty eight"/>
<number Num="29" Word="Twenty nine"/>
<number Num="30" Word="Thirty " />
<number Num="31" Word="Thirty one" />
<number Num="32" Word="Thirty two" />
<number Num="33" Word="Thirty three" />
<number Num="34" Word="Thirty four" />
<number Num="35" Word="Thirty five" />
<number Num="36" Word="Thirty six" />
<number Num="37" Word="Thirty seven" />
<number Num="38" Word="Thirty eight" />
<number Num="39" Word="Thirty nine" />
<number Num="40" Word="Forty " />
<number Num="41" Word="Forty one" />
<number Num="42" Word="Forty two" />
<number Num="43" Word="Forty three" />
<number Num="44" Word="Forty four" />
<number Num="45" Word="Forty five" />
<number Num="46" Word="Forty six" />
<number Num="47" Word="Forty seven" />
<number Num="48" Word="Forty eight" />
<number Num="49" Word="Forty nine" />
<number Num="50" Word="Fifty " />
<number Num="10" Word="Ten " />
<number Num="20" Word="Twenty " />
<number Num="60" Word="Sixty " />
<number Num="70" Word="Seventy " />
<number Num="80" Word="Eighty " />
<number Num="90" Word="Ninety " />
</xsl:variable>

<xsl:template match="/input">
    <xsl:for-each select="entry">
    <xsl:value-of select="$numbertoword/number[@Num = current()]/@Word"/>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Output: Fourty Six

I have taken word number into variable from which i comparing number from my input file and behalf of them i am getting right output but I want made my output more dynamic.

CodePudding user response:

XPath (3.1 at least, don't remember whether 2.0 also has it) provides e.g. w as the flag for e.g. format-integer(46, 'w') to spell out the number as a word.

CodePudding user response:

I got my solution by using xsl:number/

<xsl:number value="." format="w" lang="en"/>

  • Related