XSLT: Converting a number into a character?

  • Thread starter Andrzej Jan Taramina
  • Start date
A

Andrzej Jan Taramina

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!!!
 
C

C. M. Sperberg-McQueen

Andrzej Jan Taramina said:
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
 
R

Richard Tobin

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
 
A

Andrzej Jan Taramina

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!!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top