Thursday, 1 March 2018

XSLT: Format number as an alphabetic value

Here is a useful pair of functions that will format a numeric value as an alphabetic value. Ideal for generating alphabetic list numbers from integers. This is not limited to the 26 alphabetic characters and will recurse through the alphabet for each multiple of 26 such that the list will grow from a-z, aa-zz, aaa-zzz etc


 <xsl:function name="xslttricks:format-number-as-alpha">
  <xsl:param name="number" as="xs:decimal"/> 
  <xsl:value-of select="leg:format-number-as-alpha($number, ())"/>

 <xsl:function name="xslttricks:format-number-as-alpha">
  <xsl:param name="number" as="xs:decimal"/>
  <xsl:param name="case" as="xs:string?"/>
  <xsl:variable name="int" select="xs:integer(round($number))"/>
  <xsl:variable name="mod" select="$int mod 26"/>
  <xsl:variable name="times" select="xs:integer(floor($int div 26) +1)"/>
  <xsl:variable name="alpha" select="('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')"/>
  <xsl:variable name="numberstring" select="string-join((for $n in 1 to $times return $alpha[$mod]), '')"/>
  <xsl:value-of select="if ($case = ('upper', 'uppercase')) then upper-case($numberstring) else $numberstring"/>

No comments:

Post a Comment