XSLT FAQ

How can I URL encode data?

I would like to use strings as part of a link address (URL), but writing the following XSLT does not generate the result I desire, if the value of @Title contains characters like '?' or '#' - and XSLT 1.0 does not contain any functions for URL encoding.

<a href="?title={@Title}">sample like</a>

Does C1 offer any solution to this problem?

Answer:

C1's XSLT feature allow you to execute C# functions 'inline' and C# contains a large number of functions for web oriented encoding and decoding. The following XSLT show how C# is invoked to URL encode a string:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl in msxsl csharp" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csharp="http://c1.composite.net/sample/csharp" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:template match="/">
    <html>
      <head />
      <body>
        <a href="?{csharp:UrlEncodeUnicode('Is C# url friendly?')}">
          Link with reserved characters
        </a>
      </body>
    </html>
  </xsl:template>
  <msxsl:script implements-prefix="csharp" language="C#">
    <msxsl:assembly name="System.Web" />
    <msxsl:using namespace="System.Web" />

    public String UrlEncodeUnicode(string source)
    {
        return HttpUtility.UrlEncodeUnicode(source);
    }
  </msxsl:script>
</xsl:stylesheet>