XSLT: Converting a number into a character?

Discussion in 'XML' started by Andrzej Jan Taramina, Jan 29, 2004.

  1. I have a need to convert a number into the character that it
    represents.

    For example, if I have:

    <xsl:variable name="number" select="169"/>

    I want to be able to convert this to the character which is
    represented by the number( in this example, it would be the hardcoded
    character © the copyright character) for output.

    What I would like to do is something like:

    <xsl:value-of select="character( $number )"/>

    where the value-of returns a single character to the output stream (in
    this case the copyright character). But there is no such function as
    character() that I have been able to find.

    There doesn't seem to be an obvious way of doing this in XSLT.

    Any ideas on how to do this?

    Thanks!



    .....Andrzej

    NOTE: Remove Spamicide(tm) before replying!!!
     
    Andrzej Jan Taramina, Jan 29, 2004
    #1
    1. Advertising

  2. Andrzej Jan Taramina <> writes:

    > I have a need to convert a number into the character that it
    > represents. ...>
    > What I would like to do is something like:
    >
    > <xsl:value-of select="character( $number )"/>
    > ...
    > There doesn't seem to be an obvious way of doing this in XSLT.


    When I need to do this, I do it this way:

    <xsl:variable name="ascii">.......<!--*
    *-->..
    ..
    ..<!--*
    *-->........<!--*
    *-->........<!--*
    *--> !"#$%&'<!--*
    *-->()*+,-./<!--*
    *-->01234567<!--*
    *-->89:;<=>?<!--*
    *-->@ABCDEFG<!--*
    *-->HIJKLMNO<!--*
    *-->PQRSTUVW<!--*
    *-->XYZ[\]^_<!--*
    *-->`abcdefg<!--*
    *-->hijklmno<!--*
    *-->pqrstuvw<!--*
    *-->xyz{|}~</xsl:variable>

    ....

    <xsl:value-of select="substring($ascii,$number,1)"/>

    There may be more elegant ways; in the meantime, I hope this helps.

    -C. M. Sperberg-McQueen
     
    C. M. Sperberg-McQueen, Jan 29, 2004
    #2
    1. Advertising

  3. In article <>,
    Andrzej Jan Taramina <> wrote:
    >I have a need to convert a number into the character that it


    >What I would like to do is something like:
    >
    > <xsl:value-of select="character( $number )"/>
    >
    >where the value-of returns a single character to the output stream (in
    >this case the copyright character). But there is no such function as
    >character() that I have been able to find.


    Annoying, isn't it :)

    You could use a string of all the (necessary) characters as Michael
    suggests, or you could do something like:

    <xsl:text disable-output-escaping="yes">&amp;#</xsl:text>
    <xsl:value-of select="$number"/>
    <xsl:text>;</xsl:text>

    which is, of course, an even grosser hack, but has the advantage of
    not requiring a very long string if you use arbitrary Unicode characters.

    -- Richard
    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!
     
    Richard Tobin, Jan 29, 2004
    #3
  4. Solved: XSLT: Converting a number into a character?

    On 29 Jan 2004 23:08:15 GMT, (Richard Tobin) wrote:

    > Annoying, isn't it :)
    >
    > You could use a string of all the (necessary) characters as Michael
    > suggests, or you could do something like:


    That won't work since I do not know what range the character code will be in,
    and since it's Unicode....building the lookup table would be huge!

    > <xsl:text disable-output-escaping="yes">&amp;#</xsl:text>
    > <xsl:value-of select="$number"/>
    > <xsl:text>;</xsl:text>
    >
    > which is, of course, an even grosser hack, but has the advantage of


    This doesn't work either, since I'm in a Cocoon pipeline which is just
    propagating SAX events. The XML is never written out! What your suggestion
    will do (I tried it before I posted my initial request) is write "ʞ" into
    the SAX stream, which will NOT work for my needs, since I actually need the
    single character written out.

    That being said, I did find a quick/elegant solution when using Xalan. I used a
    Xalan extension to enable me to call a core Java class to do the code to
    character conversion for me. The callable template (charUtilities.xsl) looks
    somewhat like this:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xalan="http://xml.apache.org/xalan"
    xmlns:char="http://xxx.com/char"
    xmlns:charUtil="http://xxx.com/charUtil"
    extension-element-prefixes="char charUtil">

    <xalan:component prefix="charUtil">
    <xalan:script lang="javaclass" src="xalan://java.lang.Character"/>
    </xalan:component>

    <xsl:template name="char:convertNumber">
    <xsl:param name="number" select="'160'" />

    <xsl:value-of select="charUtil:new( number( $number ) )"/>
    </xsl:template>
    </xsl:stylesheet>


    To then use the conversion template you do something like the following:

    <xsl:import href="charUtilities.xsl" />

    ......

    <xsl:call-template name="char:convertNumber">
    <xsl:with-param name="number" select="670"/>
    </xsl:call-template>


    The only real downside with this approach is that you end up tied to Xalan,
    which in my case is not a big deal.


    .....Andrzej

    NOTE: Remove Spamicide(tm) before replying!!!
     
    Andrzej Jan Taramina, Feb 2, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kittykat
    Replies:
    2
    Views:
    4,825
    kittykat
    Dec 1, 2004
  2. jkflens
    Replies:
    2
    Views:
    1,497
    jkflens
    May 30, 2006
  3. ruds
    Replies:
    3
    Views:
    5,605
    Daniel Pitts
    Jan 10, 2007
  4. sweetone
    Replies:
    1
    Views:
    426
    Andrew Thompson
    Jan 20, 2007
  5. Jimbo
    Replies:
    1
    Views:
    130
    Martin Honnen
    Dec 20, 2005
Loading...

Share This Page