Displaying a Multi-Select Column "Nicely" in a DVWP with Web Services

<UPDATE dateTime=”2011-06-29T18:11Z”>I fixed a bug in the template where it didn’t always show the final value in the multi-select.</UPDATE>

In a previous post entitled Displaying a Multi-Select Column “Nicely”, I gave a recursive XSL function which split apart the values in a multi-select column and gave you the option to separate them with any separating text you chose.

In the comments on that post, I recently was asked how to adapt that function to work for multi-select values that are returned by the Lists Web Service (GetListItems).  The values that GetListItems passes back are separated differently, like this:

247;#Miami;#1;#Abington;#2;#Acton;#3;#Acushnet;#4;#Adams;#5;#Agawam;#6;#Amesbury;#7;#Amherst;#8;#Amherst Center

and here’s the XSL to format it “nicely”:

<xsl:template name="MultiSelectDisplay">
  <xsl:param name="MultiSelectValue"/>
  <xsl:param name="MultiSelectSeparator"/>
  <xsl:choose>
    <xsl:when test="contains($MultiSelectValue, ';#')">
      <xsl:choose>
        <xsl:when test="string-length(substring-before(substring-after($MultiSelectValue, ';#'), ';#')) &gt; 0">
          <xsl:value-of select="concat(substring-before(substring-after($MultiSelectValue, ';#'), ';#'), $MultiSelectSeparator)" disable-output-escaping="yes"/>
          <xsl:call-template name="MultiSelectDisplay">
            <xsl:with-param name="MultiSelectValue" select="substring-after(substring-after($MultiSelectValue, ';#'), ';#')"/>
            <xsl:with-param name="MultiSelectSeparator" select="$MultiSelectSeparator"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="substring-after($MultiSelectValue, ';#')" disable-output-escaping="yes"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring-after($MultiSelectValue, ';#')" disable-output-escaping="yes"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

It just required some simple tweaks to get it to deal with the ‘;#’ separators rather than the ‘;’ separator.

Similar Posts

8 Comments

  1. I looked at both examples. I dont know if I’m doing something wrong, but its not working for me.
    I have the same type of data as in your example: 6;#Values;#7;#Values;#2;#​Values

    I need to strip out the index numbers as well as the ;# and create a new line between the values.

    Can you please help me.
    Thanks,
    Ninel

    1. Ninel:

      When you say it’s not working, what is happening? Are you getting an error? Output that isn’t in the form you want? Can you post how you are using the template? It’s a little hard for me to help without the details.

      M.

  2. Sorry about that… So I used the following (I played around with it since my last post):

    And in the RowView:

    It rendered as: 2Data 3Data Almost there….

    Using the code above how can I include removing the index numbers at the beginning of each value and then also separate them either by a new line or comma?

    Thank you so much for responding. I’ve been banging my head against the wall for the past week.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.