Search code examples
xsltconvertersletters-and-numbers

Increment Letter's in XSL 1.0


I have from XML for example the String "AA" and i need to increment it Like:

AA AB AC AD AE (...) AZ BA BB (...)

For example increment it for 35 times (coming from a varaible called (`xsl:variable name ="NumIncr">)) starting at AA and finishing in BG. The string don't always have "AA" can be any 2 letters...

Any idea to do this?! I think the <xsl:number> tag can help, but still the problem to pass from letters to numbers.

Need something like

<fo:table-cell border-collapse="collapse" border-color="gray" font-family="Helvetica" font-size="8pt" border="solid 1pt gray" padding="1pt" display-align="before">                                                                                             
     <fo:block text-align="center">                                                                                             
          <xsl:value-of select="string($Sequence)"/>
     </fo:block>    
</fo:table-cell>

Where $sequence is the AA AB AC (...) Can writte evrything in the same cell, the problem isn't the output but the tamplate to increment the AA

HELP!!!


Solution

  • To translate a string like "AA" to its numerical value, you can use the following template:

    <xsl:template name="string-to-num">
        <xsl:param name="string"/>  
        <xsl:param name="alpha" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/> 
        <xsl:param name="magnitude" select="1"/>
        <xsl:param name="carryover" select="0"/>
        <xsl:param name="bit" select="substring($string, string-length($string), 1)"/>  
        <xsl:param name="bit-value" select="string-length(substring-before($alpha, $bit)) + 1"/>
        <xsl:variable name="return" select="$carryover + $bit-value * $magnitude"/>
        <xsl:choose>
            <xsl:when test="string-length($string) > 1">
                <xsl:call-template name="string-to-num">
                    <xsl:with-param name="string" select="substring($string, 1, string-length($string) - 1)"/>
                    <xsl:with-param name="magnitude" select="string-length($alpha) * $magnitude"/>
                    <xsl:with-param name="carryover" select="$return"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$return" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

    Some examples of calling the template:

    <xsl:call-template name="string-to-num">
        <xsl:with-param name="string">A</xsl:with-param>
    </xsl:call-template>
    

    returns 1;

    <xsl:call-template name="string-to-num">
        <xsl:with-param name="string">Z</xsl:with-param>
    </xsl:call-template>
    

    returns 26;

    <xsl:call-template name="string-to-num">
        <xsl:with-param name="string">AA</xsl:with-param>
    </xsl:call-template>
    

    returns 27;

    <xsl:call-template name="string-to-num">
        <xsl:with-param name="string">ZZ</xsl:with-param>
    </xsl:call-template>
    

    returns 702;

    <xsl:call-template name="string-to-num">
        <xsl:with-param name="string">AAA</xsl:with-param>
    </xsl:call-template>
    

    returns 703.

    These results are the exact reverse of:

    <xsl:number value="$return" format="A"/>