Unlocking the Mysteries of Data View Web Part XSL Tags – Part 10 – <xsl:choose>

This entry is part 10 of 21 in the series Unlocking the Mysteries of Data View Web Part XSL Tags

Cross-posted from EndUserSharePoint.com

<xsl:choose>
Like <xsl:if>, but with multiple possibilities, more like if-then-else.

<xsl:when>
Used within <xsl:choose> as a conditional test.  If the test is true, then the contained code is executed and the <xsl:choose> is exited.

<xsl:otherwise>
Used within <xsl:choose> as the “fall-back” condition. If none of the prior <xsl:when>s have been executed, the code contained here is executed.

I’m going to break with tradition on this post and talk about three XSL tags at once.  <xsl:choose>, <xsl:when>, and <xsl:otherwise> are sort of triplets anyway; they always travel together.

<xsl:choose> provides you a level of functionality which is more complex than <xsl:if>.  As I mention above, <xsl:choose> is more like the if-then-else construct that we’re all familiar with.  You can even have multiple <xsl:when>s to allow you to cover many different else conditions.

The most common way you’ll end up with an <xsl:choose> in your XSL is when you use the Conditional Formatting option in SharePoint Designer.  In the simplest case, you’ll be saying something like “If condition X is true, then do Y, else do Z”.  If you haven’t done any programming before, this might seem daunting, but it’s nothing different that the little deals we make with our kids: “Eat your dinner or you can’t have any dessert.”  Just the construction might be a little different.  So you’d say it like “If you eat dinner, then you get dessert, else you go to bed.”  You don’t have the threaten bedtime with an <xsl:choose>; you can just leave the <xsl:otherwise> empty.  (That case is effectively just what an <xsl:if> does.)

Enough with the abstract examples.  Say you want to display the Title of an item in bold if it starts with the letter “A”.  Perhaps not the most exciting example, but it should be simple to follow:

<xsl:choose>
<xsl:when test=”starts-with(@Title, 'A')”>
<b><xsl:value-of select=”@Title”/></b>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”@Title”/>
</xsl:otherwise>
</xsl:choose>

So we’re rendering the value of the Title column whether it starts with “A” or not, but if it does start with “A”, we’re enclosing the value in the bold tags.  Simple enough, right?

Let’s look at a more complicated (and hopefully more useful) example.  I like to use DVWPs to provide dashboards.  No KPI lists for me; I like to do all the heavy lifting with the XSL.  Let’s say that we have a target sales goal of $1,000,000 for a sales region.   It’s a stretch goal, so we’re going to consider $750,000 good, too.  $500,000 is not so good; we’re going to have to take a good look at what’s going on there.  Anything less than $500,000 is just plain awful; heads are going to roll.

<xsl:choose>
<xsl:when test=”@Sales &gt; 1000000”>
<span style=”color:green; font-weight:bold;”><xsl:value-of select=”@Sales”/></span>
</xsl:when>
<xsl:when test=”@Sales &gt; 750000”>
<span style=”color:green;”><xsl:value-of select=”@Sales”/></span>
</xsl:when>
<xsl:when test=”@Sales &gt; 500000”>
<span style=”color:yellow;”><xsl:value-of select=”@Sales”/></span>
</xsl:when>
<xsl:otherwise>
<span style=”color:red;”><xsl:value-of select=”@Sales”/></span>
</xsl:otherwise>
</xsl:choose>

Here you can start to see the power of the <xsl:choose> construct.  You can “stack” the conditions in the order you’d like them to be evaluated.  As soon as one of the conditions evaluates to true, that <xsl:when>’s internal code is executed, and then the <xsl:choose> exits.  If *none* of the <xsl:when> conditions are met, then we ‘fall out” to the <xsl:otherwise> condition.  Keep in mind that, as I said above, you don’t have to have anything in the <xsl:otherwise> clause.

clip_image002Note that when we want to use certain characters in our XSL, we must “encode” them.  This is because those characters have special meaning in XSL.  These characters are:

  • < – The “less than sign” – &lt;
  • > -The “Greater than sign”  &gt;
  • “ – The “quote” character – &quot;
  • ‘ – The “apostrophe” character – &apos;
  • & – The “ampersand” character -&amp;

There’s really no other way to put it: dealing with this encoding is a real pain in the behind until you get used to it.  It’s also one of the big reasons why people see that dreaded “Unknown Error” when they start diving into the XSL.

Changing the color of text is just one simple example of what you can do.  The <xsl:when> clauses can contain calculations (“If I’m in the East Region, then my commission should be 25% of sales; if I’m in the Central Region, my commission should be 60% of gross profit;…”)

You’ll see that I’ve used the <xsl:value-of> tag above to output values, and yes you guessed it, I’ll go over that tag in the next article.

Next up: <xsl:value-of>
Outputs the value to which it evaluates, whether it be the value of a column, a variable, etc.

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 9 – <xsl:if>Unlocking the Mysteries of Data View Web Part XSL Tags – Part 11 – <xsl:value-of> >>

Similar Posts

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.