Hiding an empty rich text column in XSLT

This morning I was trying to create a new style in the itemstyle.xsl stylesheet to use within the content query web part (CQWP). I needed a custom style to display a list of announcements, some of which had content and others didn’t and this style was to improve this output.

The problem is that the ‘Body’ column of an announcement or more importantly the ‘rich text’ field type is never really empty. Even when the column genuinely empty and has no rich text content, a hidden html element (a div) exists and acts as a wrapper for any content. As a result, if you try and use a typical ‘if equals null’ statement to hide the rich text column it won’t work because of this hidden element.

Examples

An empty rich text column on SharePoint 2010 always has 37 characters as shown below.

Empty rich text column in SharePoint 2010
Empty rich text column in SharePoint 2010

With SharePoint 2007 the rich text column has 65 characters when empty, again as shown below.

Empty rich text column in SharePoint 2007
Empty rich text column in SharePoint 2007

Solution

The solution in the end was to use the string-length function to determine if the rich text column was longer than the standard 37 characters on SharePoint 2010 as identified above.

[code lang=”powershell”]
// SharePoint 2007 = 59
// SharePoint 2010 = 37
<!– any conditional formatting would go here –>
<xsl:if test="string-length(@Body) &gt; 37">
<!– conditional formatting and presentation would go here –>
<div class="item body">
<xsl:value-of disable-output-escaping="yes" select="@Body"/>
</div>
</xsl:if>
[/code]