469,310 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,310 developers. It's quick & easy.

XSLT: Converting a number into a character?

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!!!
Jul 20 '05 #1
3 9570
Andrzej Jan Taramina <an*****@chaeron.spamicide.com> 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
Jul 20 '05 #2
In article <lg********************************@4ax.com>,
Andrzej Jan Taramina <an*****@chaeron.spamicide.com> 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!
Jul 20 '05 #3
On 29 Jan 2004 23:08:15 GMT, ri*****@cogsci.ed.ac.uk (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!!!
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Charles Chow | last post: by
5 posts views Thread by inquirydog | last post: by
2 posts views Thread by Lionel Fourquaux | last post: by
2 posts views Thread by Sharkie | last post: by
15 posts views Thread by allthecoolkidshaveone | last post: by
12 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.