[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: URL encoding

Subject: Re: URL encoding
From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 20 May 2002 04:58:40 -0600 (MDT)
xml url encoding
Zack Angelo wrote:
> Is there a quick way to encode URLs in XSL? 
> Ex: Convert http://myurl.com/document.html?param1=foo1&param2=foo2 to
> http%3A%2F%2Fmyurl.com%2Fdocument.html%3Fparam1%3Dfoo1%26param2%3Dfoo2 

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="http://www.w3.org/1999/XSL/Transform"    

  <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="http://skew.org/printenv?foo={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:  http://skew.org/xml/stylesheets/url-encode/

Have fun.

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

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

Current Thread
  • URL encoding
    • Zack Angelo - Sun, 19 May 2002 21:13:39 -0400 (EDT)
      • Michael Kay - Mon, 20 May 2002 04:01:04 -0400 (EDT)
      • Mike Brown - Mon, 20 May 2002 07:08:45 -0400 (EDT) <=
        • Mike Brown - Mon, 20 May 2002 15:52:46 -0400 (EDT)
        • bryan - Wed, 22 May 2002 06:51:35 -0400 (EDT)


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.