Cleaning up Multi Choice fields in Content Query Web Part

I recently had to put together a Content Query Web Part that displayed a multiple choice field. By default the multiple choice field displays delimited with “;#”. Additionally sometimes you will find this combination of characters on the beginning and the ending of the string. Here is my XSLT template to clean up the choice field and substitute your replacement characters. Additionally I split out some of the supporting templates as they are very useful on their own.

Usage

<xsl:variable name="CleanFieldValue">
     <xsl:call-template name="cleanMultiChoice">
          <xsl:with-param name="choiceValue" select="@FieldValue"/>
     </xsl:call-template>
</xsl:variable>
 
<xsl:call-template name="replaceCharsInString">
     <xsl:with-param name="stringIn" select="$CleanFieldValue"/>
     <xsl:with-param name="charsIn" select="';#'"/>
     <xsl:with-param name="charsOut" select="', '"/>
</xsl:call-template>

Templates

<xsl:template name="replaceCharsInString">
	<!-- Source: http://www.dpawson.co.uk/xsl/sect2/replace.html -->
	<xsl:param name="stringIn"/>
	<xsl:param name="charsIn"/>
	<xsl:param name="charsOut"/>
	<xsl:choose>
		<xsl:when test="contains($stringIn,$charsIn)">
			<xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
			<xsl:call-template name="replaceCharsInString">
				<xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
				<xsl:with-param name="charsIn" select="$charsIn"/>
				<xsl:with-param name="charsOut" select="$charsOut"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$stringIn"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
 
<xsl:template name="trimEnd">
	<xsl:param name="stringIn"/>
	<xsl:param name="chars"/>
	<xsl:choose>
		<xsl:when test="substring($stringIn, (string-length($stringIn) - string-length($chars)) + 1) = $chars">		
			<xsl:value-of select="substring($stringIn, 0, (string-length($stringIn) - string-length($chars)) + 1)"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$stringIn"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
 
<xsl:template name="cleanMultiChoice">
	<xsl:param name="choiceValue"/>	
	<xsl:variable name="string">
		<xsl:call-template name="trimEnd">
			<xsl:with-param name="stringIn" select="$choiceValue"/>
			<xsl:with-param name="chars" select="';#'"/>
		</xsl:call-template>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="starts-with($string,';#')">		
			<xsl:value-of select="substring-after($string,';#')"/>
		</xsl:when>
		<xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
	</xsl:choose>
</xsl:template>
I'm a public speaker and the Chief SharePoint Architect for Eastridge, a Microsoft Gold Partner specializing in SharePoint and custom application development in Winston-Salem, NC. I focus on the SharePoint platform with a specialty in Information Architecture, Publishing and Best Practices.

Got something to say? Go for it!