Name

MultiSelectValueCheck

Notes

This template checks to see if a specific value is present in a multi-value column. You pass it a multi-select column value, the value to check for, the delimiter, and the values you would like returned if the match is true or false. The latter two parameters allow you to perform more functions or display certain values depending on whether the value matches or not.

The template is intentionally written verbosely to make it as understandable as possible. You could certainly combine all of the positive tests into one xsl:when test. See Filtering on a Value in a Multi-Select Lookup Column for more information.

Example

This example would determine if the value of the variable State is present in the multi-select StatesInRegion column value. If so, 1 is returned, else 0, with that value assigned to the variable StateIsInRegion.
<xsl:variable name="StateIsInRegion">
  <xsl:call-template name="MultiSelectValueCheck"> 
    <xsl:with-param name="MultiSelectColumn" select="@StatesInRegion"/>
    <xsl:with-param name="CheckValue" select="$State"/>
    <xsl:with-param name="Delimiter" select="';'"/>
    <xsl:with-param name="ReturnTrue" select="1"/>
    <xsl:with-param name="ReturnFalse" select="0"/>
  </xsl:call-template>
</xsl:variable>

Code

    <xsl:template name="MultiSelectValueCheck"> 
        <xsl:param name="MultiSelectColumn"/> 
        <xsl:param name="CheckValue"/> 
        <xsl:param name="Delimiter"/> 
        <xsl:param name="ReturnTrue"/> 
        <xsl:param name="ReturnFalse"/> 
        <xsl:variable name="Match"> 
            <xsl:choose> 
                <!-- Exact match --> 
                <xsl:when test="$MultiSelectColumn = $CheckValue"> 
                    <xsl:value-of select="$ReturnTrue"/> 
                </xsl:when> 
                <!-- First of multiple values match --> 
                <xsl:when test="starts-with($MultiSelectColumn, concat($CheckValue, $Delimiter))"> 
                    <xsl:value-of select="$ReturnTrue"/> 
                </xsl:when> 
                <!-- Match in the middle of multiple values --> 
                <xsl:when test="contains($MultiSelectColumn, concat($Delimiter, $CheckValue, $Delimiter))"> 
                    <xsl:value-of select="$ReturnTrue"/> 
                </xsl:when> 
                <!-- Last of multiple values match --> 
                <xsl:when test="substring($MultiSelectColumn, string-length($MultiSelectColumn) - string-length(concat($Delimiter, $CheckValue)) + 1, string-length(concat($Delimiter, $CheckValue))) = concat($Delimiter, $CheckValue)"> 
                    <xsl:value-of select="$ReturnTrue"/> 
                </xsl:when> 
                <!-- No match --> 
                <xsl:otherwise> 
                    <xsl:value-of select="$ReturnFalse"/> 
                </xsl:otherwise> 
            </xsl:choose> 
        </xsl:variable> 
        <xsl:value-of select="$Match"/> 
    </xsl:template>