Re: [xsl] URL encoding

Subject: Re: [xsl] URL encoding
From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 20 May 2002 04:58:40 -0600 (MDT)
Zack Angelo wrote:
> Is there a quick way to encode URLs in XSL? 
> Ex: Convert to

To apply 'URL-encoding' to a string, so that you can safely embed that string
in a hier_part or opaque_part of a URI, (you don't really want to apply it to
an entire URI, unless that's the string being embedded), you can use either an
extension function or an XSLT template that parses the string and encodes as

First, you should note that URL-encoding is only 100% safe for characters that
fall in the ASCII range (32-126, or 0-127 if you want to count control
characters). Outside of this range, it gets tricky, because URIs were not
intended to encapsulate arbitrary binary data. However, if the string contains
some non-ASCII characters, newer standards are recommending that the UTF-8
bytes for those characters should be the basis for the encoding. For example,
a small e with acute accent would be %C3%A9. This gives you the freedom to
have any of the million+ possible Unicode characters in your string.

Depending on what you're doing, though, you'll find that many applications
that process URIs are in fact expecting that ISO-8859-1, not UTF-8, is the
basis for the encoding. The e with acute accent would need to be encoded as
%E9, for example. This limits you to a very small range of Unicode in your
string, just the first 256 characters out of 1.1 million, but this might be
enough for you, I don't know.

If you want to make the iso-8859-1 assumption, or if you don't care either way
because you're only dealing with ASCII strings, then that makes life easy.
With that being the case...

If your XSLT processor is Java based, it probably has a namespace reserved for
invoking static methods in arbitrary classes in your classpath, and this would

<xsl:stylesheet xmlns:xsl="";    

  <xsl:output method="html" indent="yes"/>

  <xsl:param name="str" select="'encode me #1 superstar?'"/>
  <xsl:template match="/">
    <xsl:if test="function-available('url:encode')">
      <a href="{url:encode($str)}">
        <xsl:value-of select="$x"/> 


If you want a more portable, pure XSLT 1.0 approach, here's some
voodoo for you:

Have fun.

   - Mike
  mike j. brown                   |  xml/xslt:
  denver/boulder, colorado, usa   |  resume:

 XSL-List info and archive:

Current Thread