Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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:

share|improve this answer
    
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 –  Eugene Katz 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. –  Andrey Markeev 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. –  Eugene Katz Sep 20 '11 at 13:58
    
Ended up using JavaScript and it works. –  Eugene Katz 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
up vote 1 down vote accepted

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>");]]>
    </script>

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

share|improve this answer

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

share|improve this answer
2  
Welcome to SharePoint! Since this is your first post, I'm not familiar with your account. We require relevant answers and disclosure of any affiliation with products or websites. Please see May I promote products or websites I am affiliated with here? for guidelines. –  Kit Menke Sep 20 '11 at 13:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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