Wednesday, August 12, 2009

SharePoint XSLT data view : issues rendering special characters (e.g. &, >, ") and line breaks

I have had problem with my custom display form, where displaying the content of a multiple line text box field does not render the special characters (such as &, >, ") and line breaks properly. After Googling for a couple of hours, there were no solution to my problem. Many suggested using "disable-output-escaping" to fix the special character issue, and some suggested using select="ddwrt:AutoNewLine(string(@Description))" to fix the line breaks issue. They cannot co-exist in the "xsl:value-of" tag because the AutoNewLine function will overwrite the special characters with their HTML encoding (see here).

Finally, I have worked out a solution to resolve both issues. Adding the following template (function) to your display form ASPX and calling it when display a multiple line text field. It replaces the new line character (\n or &#xa;) with a <br>.

Add this function to your DispForm.aspx file:
<xsl:template name="formatMultiLineText">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '&#xa;')">
<xsl:value-of select="substring-before($text, '&#xa;')" disable-output-escaping="yes"/>
<br/>
<xsl:call-template name="formatMultiLineText">
<xsl:with-param name="text" select="substring-after($text,'&#xa;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Where your multiple line text is displaying, replace this:
<xsl:value-of select="@Description">

with this:
<xsl:call-template name="formatMultiLineText">
<xsl:with-param name="text" select="@Description">
</xsl:call-template>
Although it might be a bit of work to replace all the multi-line text fields with the template call, it will finally give you some peace of mind. I'd be happy to know if you have any better ideas to overcome all these hassles. :)

No comments:

Post a Comment