translate()-function (Xalan)

Subject: translate()-function (Xalan)
From: Peter Paulus <paulus@xxxxxxxx>
Date: Tue, 15 Aug 2000 14:32:33 +0200
Hi,

In our application, the resulting 'xml' should have several different
character encodings. The exact encoding depends on the font being used.

The input xml always contains 'Unicode' encoding.

Ideally one would like to use the 'encoding' attribute on the
<xsl:output>-element. But this has several disadvantages:
1. The result encoding is non-standard. Several encoders would have to be
programmed.
2. There is no a priori knowledge about the exact fonts that are going to be
used.
3. It looks to me that you can select only 1 output encoding, whereas I
would need several.

Alternatively one could use the (XPath) translate()-function. I started out
with a 'hardcoded' <xsl:value-of select='translate(., "&#xE9;", "&#x8E;")'/>
or <xsl:value-of select='translate(title, "&#xE9;", "&#x8E;")'/>. This
works.

However, (worst case) I will need, say up to 10 different encodings in the
resulting xml. My example only contains a single mapping. In real life there
will somewhere around 30 mappings per encoding.

So, I figured, why not put the encodings in global variables and pass those
to the xpath function translate(). Here's what I did:

<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:transform version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='xml' encoding='ISO-8859-1' omit-xml-declaration='no'
media-type='application/qxml'/>

<xsl:variable name='Unicode_Encoding'>&#xE9;</xsl:variable>
<xsl:variable name='FontXYZ_Encoding'>&#x8E;</xsl:variable>

<xsl:template match='/'>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match='journey/title[@level = 1]'>
<paragraph style='Caption 1'>
<xsl:value-of select='translate(., "{Unicode_Encoding}",
"{FontXYZ_Encoding}")'/>
<xsl:if test='position() != last()'><br/></xsl:if>
</paragraph>
</xsl:template>

</xsl:transform>

This indeed does translate() something: every U becomes F, every e becomes
Z. translate() interprets the second and third argument as literal strings.
How can one pass the variables to the translate function?

Are there any other alternatives in achieving the encoding mappings?

With kind regards,
Peter Paulus


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread