I am using the XML Viewer Web Part to display some links. I need to encode the URI component because a value of a parameter may contain and ampersand. The way to do this in XSLT 2.0 is by using fn:encode-for-uri(). However, as soon as I add this to the XSL, the web part returns the error:

Failed to apply XSLT to the content.


In SharePoint we have XSLT 1.0, so the 2.0 functions obviously will not work.

Instead, you can use some SharePoint stuff, and in this particular situation ddwrt:UrlEncode extension function will probably help you. For details, please, read following MSDN article:

  • 1.0? that's odd because as if I change the version from 2.0 to 1.0, the XSLT stops working. I've considered ddwrt, but haven't figured if I can use it in an XML web part, and if so, how. I'll try this: blogs.pointbridge.com/Blogs/johnson_bert/Pages/Post.aspx?_ID=5 Sep 20 '11 at 12:42
  • In my case version="1.0", applied to the <xsl:stylesheet>, works fine. However, ddwrt functions don't seem to work there, even if I provide correct namespace. After some investigations, including even Reflector, turns out, that the XmlWebPart is actually a very odd webpart and it is separated completely from all other SharePoint Xslt webparts, like DVWP, XLV, CQWP, etc. So I confess, my answer is wrong: you can't use ddwrt:UrlEncode in XML Viewer. I'll try to find another solution. Sep 20 '11 at 13:56
  • I was just playing with it and came to the same conclusion. Was able to switch to 1.0 as well; error must have been dues to something else. Sep 20 '11 at 13:58
  • Ended up using JavaScript and it works. Sep 21 '11 at 19:47
  • This worked for my SharePoint 2010 xslt. Was able to paste <xsl:value-of select="ddwrt:UrlEncode(@Title)"/> directly into my custom DisplayForm.aspx page. No JS required for my purposes
    – bkwdesign
    Mar 13 '13 at 19:43

I ended up using JavaScript to handle the encoding, since it seems to be the only option. Passing XSL variables to JavaScript and then outputting the link with document.write().

    <script type="text/javascript">
        <xsl:text>var urlPrefix = "</xsl:text><xsl:value-of select="$urlPrefix"/><xsl:text>";</xsl:text>
        <xsl:text>var valueToEncode = "</xsl:text><xsl:value-of select="$valueToEncode"/><xsl:text>";</xsl:text>
        <![CDATA[document.write("<a href='"+urlPrefix+encodeURIComponent(valueToEncode)+"'>link text</a>");]]>

Note that if your valueToEncode may contain quotes ("), you need to take additional measures.


could you not use an xml editor that covers xslt 1.0 and 2.0 like liquid xml editor?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.