Tuesday, March 01, 2005

(XSLT) Encoding URL parameters

During XSL Transformation, I had a problem where dynamically generated URL parameters contained data that needs to be URL Encoded.

example from an xsl stylesheet

<a href="http://mywebsite/servlet?param1={xmldata}">link</a>

The xml data may contain values that may not be transported correctly (e.g., &).

A solution that seems to work is

<a onclick="this.href+=encodeURIComponent('{xmldata}');" href="http://mywebsite/servlet?param1=">link</a>

Note: Javascript has different functions for encoding URLs. Refer this.

Main points to remember about the various functions are
  • escape() will not encode: @*/+
  • encodeURI() will not encode: !@#$&*()=:/;?+'
  • encodeURIComponent() will not encode: !*()'

No comments: