Unlocking the Mysteries of Data View Web Part XSL Tags – Part 10 – <xsl:choose>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 1: Overview
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 2 – <xsl:template>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 3 – <xsl:call-template>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 4 – <xsl:with-param>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 5 – <xsl:param>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 6 – <xsl:variable>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 7 – <xsl:for-each>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 8 – <xsl:sort>
- 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 10 – <xsl:choose>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 11 – <xsl:value-of>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12 – Miscellaneous: Person or Group Columns
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 13 – Miscellaneous: String Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14 – Miscellaneous: ddwrt Namespace Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15 – Miscellaneous: Field / Node Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16 – <xsl:attribute>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17 – <xsl:comment> and <xsl:text>
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18 – Miscellaneous – Some Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19 – Miscellaneous – More Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20 – <xsl:import>
- Unlocking the Mysteries of the SharePoint Data View Web Part XSL Tags eBook Now Available
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 > 1000000”> <span style=”color:green; font-weight:bold;”><xsl:value-of select=”@Sales”/></span> </xsl:when> <xsl:when test=”@Sales > 750000”> <span style=”color:green;”><xsl:value-of select=”@Sales”/></span> </xsl:when> <xsl:when test=”@Sales > 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.
Note 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” – <
- > -The “Greater than sign” >
- “ – The “quote” character – "
- ‘ – The “apostrophe” character – '
- & – The “ampersand” character -&
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.