tags:

views:

29

answers:

1

I have URLs with some special characters and i would like to replace them and I am using xslt 1.0 so I am writing the code as below.

<xsl:template name="string-replace-all"> 
        <xsl:param name="text"/> 
        <xsl:param name="replace"/> 
        <xsl:param name="by"/> 
        <xsl:choose> 
            <xsl:when test="contains($text,$replace)"> 
                <xsl:value-of select="substring-before($text,$replace)"/> 
                <xsl:value-of select="$by"/> 
                <xsl:call-template name="string-replace-all"> 
                    <xsl:with-param name="text" select="substring-after($text,$replace)"/> 
                    <xsl:with-param name="replace" select="$replace"/> 
                    <xsl:with-param name="by" select="$by"/> 
                </xsl:call-template> 
            </xsl:when> 
    <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
    </xsl:otherwise> 
  </xsl:choose> 
</xsl:template> 

and calling like this:

<td class="ms-vb">
            <xsl:variable name="link">
            <xsl:call-template name="string-replace-all"> 
                <xsl:with-param name="text" select="@FileRef"/> 
                <xsl:with-param name="replace" select="'&#39;'"/> 
                <xsl:with-param name="by" select="'%27'"/> 
            </xsl:call-template> 
            </xsl:variable>-->
                <a target="_blank" href="@link" ><xsl:value-of select="@New_x0020_Doc_x0020_Title" disable-output-escaping="yes" /></a>
            </td>

I am getting the error "webpart cannot be displayed". Can anyone please suggest what am i doing wrong here?

+1  A: 

The provided "replace" template is working.

To confirm this use the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
        <xsl:variable name="link">
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="@FileRef"/>
                <xsl:with-param name="replace">'</xsl:with-param>
                <xsl:with-param name="by" select="'%27'"/>
            </xsl:call-template>
        </xsl:variable>

        "<xsl:value-of select="$link"/>"
    </xsl:template>

    <xsl:template name="string-replace-all">
        <xsl:param name="text"/>
        <xsl:param name="replace"/>
        <xsl:param name="by"/>
        <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                <xsl:value-of select="substring-before($text,$replace)"/>
                <xsl:value-of select="$by"/>
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                    <xsl:with-param name="replace" select="$replace"/>
                    <xsl:with-param name="by" select="$by"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

when the transformation is applied on this XML document:

<t FileRef="Abc'Xyz'Tuv"/>

the wanted, correct result is produced:

"Abc%27Xyz%27Tuv"

The problem may be in the way you specify the replace parameter (uneven number of apostrophes in the expression):

Instead of:

<xsl:with-param name="replace" select="'&#39;'"/>

Use:

<xsl:with-param name="replace">'</xsl:with-param>
Dimitre Novatchev
@Dimitre: +1 Good catch! Also this would work: `<xsl:with-param name="replace" select='"'"'/>`
Alejandro
Actually we found that the problem is with sharepoint. Once the user renamed the files everything worked fine. Thanks for the reply. I hope i use this sometime later.
@user346514: Your comment is wrong. This declaration ` <xsl:with-param name="replace" select="'''"/> ` produce this error: `A string literal was not closed`.
Alejandro
@user346514: Yes, @Alejandro is right!
Dimitre Novatchev