2
votes

I'm using a "choice" site column with the multiple-check option enabled so that users can tag a list item with several choices from the column.

This column then powers a design feature in a content query webpart - where the column choice is appended to create an image filename.

  • Choice1
  • Choice2
  • Choice3
  • Choice4

becomes

<img src="http://mysite/content-Choice1.jpg />

The problem I've got is that the XSL parser is fed a string which has semicolons (;) and hashes (#) separating the choice values. If all 4 options were ticked, the string fed into the XSLT parser would be:

;#Choice1;#Choice2;#Choice3;#Choice4

How can I work through the string and separate each choice into its own XSL variable?

I've tried various substring-before functions, but I can't get anything working.

2
General xslt questions should have input source, desired output and relationship. This question is for an specific XSLT framework. As general XSLT question the answer is there is no way for How can I work through the string and separate each choice into it's own XSL variable?. You could built a temporaly result tree (with extensions functions for XSLT 1.0) holding a elements for each "Choice" string. See questions about tokenizing in XSLT. - user357812

2 Answers

2
votes

Since XPath 1.0 does not support the tokenize() function, you'll have to do all the work yourself. For instance, you can generate the <img> elements recursively from the choices:

<xsl:template name="RecurseConvertChoicesToImages">
    <xsl:param name="choices" />

    <xsl:variable name="token"
        select="substring-before($choices, ';#')" />
    <xsl:variable name="nextToken"
        select="substring-after($choices, ';#')" />

    <xsl:if test="$token">
        <img src="http://mysite/content-{$token}.jpg" />
    </xsl:if>
    <xsl:if test="$nextToken">
        <xsl:call-template name="RecurseConvertChoicesToImages">
            <xsl:with-param name="choices" select="$nextToken" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>
2
votes

I would recommend to use JavaScript to parse the string and accordingly display the image using JavaScript.