Unlocking the Mysteries of Data View Web Part XSL Tags – Part 9 – <xsl:if>

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

Cross-posted from EndUserSharePoint.com

<xsl:if>
A single conditional test. If the test is true, then the contained code is executed.

<xsl:if> is one of the simple workhorses in the Data View Web Part (DVWP) XSL arsenal.  Its entire goal in life is to determine whether the code it contains is executed or not.

Looking at our standard, basic example DVWP code, we can see several <xsl:if>s in play:

<XSL><xsl:stylesheet xmlns:x="<a href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>" xmlns:d="<a href="http://schemas.microsoft.com/sharepoint/dsp">http://schemas.microsoft.com/sharepoint/dsp</a>" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">http://schemas.microsoft.com/WebParts/v2/DataView/runtime</a>" xmlns:asp="<a href="http://schemas.microsoft.com/ASPNET/20">http://schemas.microsoft.com/ASPNET/20</a>" xmlns:__designer="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/designer">http://schemas.microsoft.com/WebParts/v2/DataView/designer</a>" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">

<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:param>'</xsl:param>
<xsl:variable>0</xsl:variable>

<xsl:template match="/">
<xsl:call-template/>
</xsl:template>

<xsl:template>
<xsl:variable>Table</xsl:variable>
<xsl:variable select="/dsQueryResponse/Rows/Row"/>
<table border="0" width="100%" cellpadding="2" cellspacing="0">
<tr valign="top">
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<th width="1%" nowrap="nowrap"></th>
</xsl:if>
<th nowrap="nowrap">Title</th>
</tr>
<xsl:call-template>
<xsl:with-param select="$Rows"/>
</xsl:call-template>
</table>
</xsl:template>

<xsl:template>
<xsl:param/>
<xsl:for-each select="$Rows">
<xsl:call-template/>
</xsl:for-each>
</xsl:template>

<xsl:template>
<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute>ms-alternating</xsl:attribute>
</xsl:if>
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<td width="1%" nowrap="nowrap">
<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
</td>
</xsl:if>
<td>
<xsl:value-of select="@Title"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet></XSL>

Here’s what each of those <xsl:if>s is up to:

<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<th width="1%" nowrap="nowrap"></th>
</xsl:if>

imageIn this <xsl:if>, we’re testing the value of the $dvt_1_automode variable, and if it is ‘1’, then the contained code will be executed, which outputs a table header cell (TH). (This header cell will be shown if you turn on any of the Edit options in the Data View Properties, as shown.) I’ve never seen a cogent explanation for what the ddwrt:cf_ignore function does; if you know, please leave a comment!

<xsl:if test="position() mod 2 = 1">
<xsl:attribute>ms-alternating</xsl:attribute>
</xsl:if>

This <xsl:if> is checking to see if the current item is in an odd or even row.  (If you are unfamiliar with the modulus, or mod, function, it returns the remainder when you divide the first value by the second.)  The position() gives us the row number in the rowset.  If the row number is odd, we apply the ms-alternating CSS class.  This is the class that shows alternating white and light grey rows in List View Web Parts (LVWPs) in SharePoint out of the box.

image

Can you tell that I just downloaded SnagIt?

<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<td width="1%" nowrap="nowrap">
<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
</td>
</xsl:if>

Finally, this <xsl:if> is checking once again for the value of $dvt_1_automode.  If it is enabled, the contained table detail cell (TD) is rendered. Once again, I’m not exactly sure about the ddwrt:am* functions used here. As I mentioned in a previous article in this series, the ddwrt namespace functions are woefully undocumented, other than in Serge van den Oever’s great article on MSDN.

The syntax for <xsl:if> is pretty simple.  Each time you use it, you need to specify a test which will evaluate to true or false. If the value is true, then the code the <xsl:if&gt; contains is executed, if the value is false, then it isn’t.

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

Series Navigation<< 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 10 – <xsl:choose> >>

Leave a Reply