XSLT conditional formatting

891 views
Skip to first unread message

scook

unread,
Jun 14, 2012, 12:12:21 PM6/14/12
to xs...@googlegroups.com
Hi,

I'm new to XSLT... I am trying to conditionally format a cell in a table exported from SSRS to excel through XSLT.  Few questions:

1. Do I need to put the conditional language in the Style section or in the table section?

2. I currently have the following in the table section, which is not working:
    <Cell ss:StyleID="s24"><Data ss:Type="String">   

<tr>
                <xsl:choose>
          <xsl:when test="@Risk &gt;='2'">
            <td><xsl:attribute
name="style">background-color:yellow;</xsl:attribute>
            <xsl:value-of select="@State"/></td>
                 </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="@State"/></td>
          </xsl:otherwise>
        </xsl:choose></tr>

</Data></Cell>

3. I will have 3 different conditions and the otherwise condition - do I need to generate a working version of the above 3 times or is there a shortcut for implementing different conditions?  My conditions 1 - Red, 2 - Yellow, 3 - Green.

Christian Wahl

unread,
Jun 17, 2012, 4:01:34 PM6/17/12
to xs...@googlegroups.com
Hi

1. I don't get the question...
2. This could be OK, but it is difficult to see without the source data. The @Risk attribute could probably used as a integer, so:

    <xsl:when test="@Risk &gt;= 2">
3. Could be simpler. Can you use the value of @Risk to name a class, like:
<xsl:attibute name="class" select="color{@Risk}"/> and then define the class style somewhere else:
.color{background-color: none;}
.color1{background-color: red;}
.color2{background-color: yellow;}

or move the <xsl:choose> to the <xsl:attribute>, like:
<tr>

  <td>
    <xsl:attribute name="style">
      <xsl:text>background-color: </xsl:text>

      <xsl:choose>
        <xsl:when test="@Risk &gt;='2'">
          <xsl:text>yellow;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>none;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:attribute>
    <xsl:value-of select="@State"/>
  </td>
</tr>
- alternative I would specify a class instead of a style attribute....
Reply all
Reply to author
Forward
0 new messages