3

I have a simple list with 3 columns: Title, URL, Color. The Color column is a choose column with two options: yellow or lightgreen. I am using the XSLT List View Web Part .

I am trying to create an xslt file with 1 conditional statement. In simple terms if the value of the item in column color=yellow format the in yellow background; otherwise format it in light green background. I am having trouble with the expression for the xsl:when

Here is the template I am using for the items:

    <xsl:template match="Item">
    <div class="item" style="background-color:lightgreen">
        <xsl:attribute name="onclick">
            window.location = '<xsl:value-of select="URL"/>'
        </xsl:attribute>
        <span class='title'><marquee direction="left" behavior="scroll"><xsl:value-of select="Title"/></marquee></span>
    </div>
</xsl:template>

So I am struggling getting the file to choose between the above and the following:

    <div class="item" style="background-color:lightgreen">
        <xsl:attribute name="onclick">
            window.location = '<xsl:value-of select="URL"/>'
        </xsl:attribute>
        <span class='title'><marquee direction="left" behavior="scroll"><xsl:value-of select="Title"/></marquee></span>
    </div>

Any suggestions appreciated. Maybe there is a simpler way of doing this. I am new to XSLT

1

Have you considered doing the following:

<xsl:choose>
  <xsl:when test="color='yellow'">
    <xsl:attribute name="style">background-color:yellow</xsl:attribute>
  </xsl:when>
  <xsl:otherwise>
    <xsl:attribute name="style">background-color:lightgreen</xsl:attribute>
  </xsl:otherwise>
</xsl:choose>

You would add this snippet right under the opening .

  • Thank you, I was missing one of the single quotations around yellow. I was initially writing as <xsl:when test="color='yellow"> instead of <xsl:when test="color='yellow'"> – Martin Jul 20 '14 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.