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>                                 

Last edited Oct 30, 2010 at 11:01 PM by sympmarc, version 9

Comments

jayakumar39 Nov 9, 2010 at 5:30 PM 
Hi i have used this template in my development, when i tried to call this template its not returning any value. i checked the template and found that its not returning the value because the <xsl:variable name="Match"> which is used not having return type. and i feel this variable is not at all required in this scenario. see my below code which is working fine.. Please update this in the code line which will help other developers in saving time. i tried around 2 hrs to get it work. by debugging the result set.
//changed code--jay

<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: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:template>


Thanks

Jay