[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] translate for partial uri-encoding?


Subject: [xsl] translate for partial uri-encoding?
From: "Jeff Greif" <jgreif@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 8 Sep 2004 21:02:43 -0700

Suppose I have this input document

<company>JOE'S PAINT &amp; BODY<company>

and I'm trying to insert the element's text in the query string of a URI in
a result document, doing something like this

<xsl:template match="/">
   <someURL>
       <xsl:text>xsl:text>http://foo.com/?X=1&amp;CO=</xsl:text>
       <xsl:value-of select="translate(/company, ' ', '+')"/>
    </someURL>
</xsl:template>

The result document should look like:
<someURL>http://foo.com/?X=1&amp;CO=JOE%27S+PAINT+%26+BODY</someURL>

The translate function arguments above are insufficient to correctly
url-encode the text of the input <company> node given above (as written, it
only converts space to +).  The proof-of-concept demo I'm producing in a
very limited time will run on an XSLT platform that does not support *any*
extensions (such as the EXSLT str:encode-uri), so I'm trying to get by with
xsl:translate.  For this demo, I have some control over what input will
actually be passed, but must at least appear to have some idea of what I'm
doing for common cases with unsafe characters.

What is the trick for including the ampersand and apostrophe characters in
the 2nd argument of the translate function and their encoded equivalents in
UTF-8 (e.g. %26 for the ampersand) in the third argument of translate?  I'm
not ambitious enough to attempt to handle all the unsafe characters that can
occur in all possible character encodings, or even all such characters in
one encoding.

Jeff


Current Thread
Keywords