title: Escaping URI paths with XSLT
body: <p>I am working on a data aggregation project in which I need to convert XML data from various data providers into a common format, which requires that all URI values in the dataset are properly escaped. Here is an example URI I receive from a data provider. It contains reserved characters ("[", "]") in its path component:</p>
  
  <pre><code>http://ogimages.bl.uk/images/001/001ROY000018D03U00107000[SVC1].jpg
  </code></pre>
  
  <p>This is the escaped form I need:</p>
  
  <pre><code>http://ogimages.bl.uk/images/001/001ROY000018D03U00107000%5BSVC1%5D.jpg
  </code></pre>
  
  <p>I planned to use XSLT with the EXSLT encode-uri function (http://www.exslt.org/str/functions/encode-uri/str.encode-uri.html) to escape URIs at conversion time. At the moment I am using it as follows:</p>
  
  <pre><code>xsl:value-of select="str:encode-uri(., true())"/&gt;
  </code></pre>
  
  <p>which escapes the complete URI and results in</p>
  
  <pre><code>http%3A%2F%2Fogimages.bl.uk%2Fimages%2F001%2F001ROY000018D03U00107000%5BSVC1%5D.jpg
  </code></pre>
  
  <p>This is not what I need because I would like to keep URI reserved characters if they are in the right place.</p>
  
  <p>If I use the encode-uri function as follows</p>
  
  <pre><code>xsl:value-of select="str:encode-uri(., false())"/&gt;
  </code></pre>
  
  <p>the brackets are not escaped because the encode-uri function doesn't escape reserved characters when being called with a boolean false flag.</p>
  
  <p>So I guess, what I need is to apply URI-encoding on the individual path components of a URI. Are there any existing functions for that or do I need to start URI parsing in XSL(T)?</p>
  
  <p>Thx</p>