This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 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
necessary.

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
work:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";    
   version="1.0"
   xmlns:url="http://whatever/java/java.net.URLEncoder";
   exclude-result-prefixes="url">

  <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"/> 
      </a>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]