Search code examples
templatesxsltxslt-2.0xslt-1.0alphabet

Transform an int to a char


I'd like to write the alphabet with a link for each letter. So I used templates but I don't how to make this letter I tried that but I had a normal mistake : (A decimal representation must imediately follow the &# in a character reference).

<xsl:template name="alphabet">
    <xsl:param name="iLetter"/>
    <xsl:if test="$iLetter &lt; 91">
        <a><xsl:attribute name="href">req.html?X_letter=&#<xsl:value-of select="$iLetter"/>;</xsl:attribute>&#<xsl:value-of select="$iLetter"/>;</xsl:attribute></a>
        <xsl:call-template name="alphabet">
            <xsl:with-param name="iLetter" select="number($iLetter)+1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

And I call this template ilke that:

<xsl:call-template name="alphabet">
        <xsl:with-param name="iLetter" select="number(65)"/>
</xsl:call-template>

So, I'd like to obtain this result:

A B C D ..... X Y Z without ... of course :)


Solution

  • As Martin suggests it would be better to avoid using disable-output-escaping. You don't need it either, if you are would be satisfied with a plain ascii character instead of the numerical character reference. If so, you can use substring and a alphabet lookup-string like this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="alphabet" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    
        <xsl:template name="alphabet"> 
            <xsl:param name="iLetter" select="65"/> 
            <xsl:if test="$iLetter &lt; 91"> 
                <a>
                    <xsl:attribute name="href">req.html?X_letter=<xsl:value-of select="substring($alphabet, $iLetter - 64, 1)"/></xsl:attribute>
                    <xsl:value-of select="substring($alphabet, $iLetter - 64, 1)"/>
                </a> 
                <xsl:call-template name="alphabet"> 
                    <xsl:with-param name="iLetter" select="number($iLetter)+1"/> 
                </xsl:call-template> 
            </xsl:if> 
        </xsl:template> 
    
        <xsl:template match="/">
            <xsl:call-template name="alphabet"/> 
        </xsl:template>
    </xsl:stylesheet> 
    

    Cheers!