1,734 articles and 13,279 comments as of Tuesday, October 19th, 2010

Thursday, February 18, 2010

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

Author: Marc D. Anderson
http://mdasblog.wordpress.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 name="dvt_apos">'</xsl:param>
<xsl:variable name="dvt_1_automode">0</xsl:variable>

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

<xsl:template name="dvt_1">
<xsl:variable name="dvt_StyleName">Table</xsl:variable>
<xsl:variable name="Rows" 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 name="dvt_1.body">
<xsl:with-param name="Rows" select="$Rows"/>
</xsl:call-template>
</table>
</xsl:template>

<xsl:template name="dvt_1.body">
<xsl:param name="Rows"/>
<xsl:for-each select="$Rows">
<xsl:call-template name="dvt_1.rowview"/>
</xsl:for-each>
</xsl:template>

<xsl:template name="dvt_1.rowview">
<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">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>

In 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 name="class">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.


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> 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.

Author: Marc D. Anderson
http://mdasblog.wordpress.com

Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA.  He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes.  Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice.  Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.

Entries in this series:
  1. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
  2. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
  3. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
  4. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
  5. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
  6. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
  7. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
  8. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
  9. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
  10. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
  11. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
  12. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
  13. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
  14. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
  15. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
  16. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
  17. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
  18. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
  19. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
  20. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
  21. EUSP eBook Store: First SharePoint Title is Now Available
 

Please Join the Discussion

10 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 9: xsl:if”
  1. Kunal says:

    Hi Marc,
    This may a little off topic.
    When we upload a new document in doc library we get a new icon displayed next to its name. But this happens only for new documents. I want to display the new icon for Folders too. Kindly help.

  2. Kunal:

    Yes, indeed, off topic. I’m also not sure of your question. Why don’t you post it with more detail in the Stump the Panel forums here.

    M.

  3. beargal says:

    Hi Marc,
    Love your comment about SnagIt. It’s a great tool and obviously you are using it well.

    One part I don’t understand is when you’re talking about Position and the Mod function. Above where you have …you state that the mod function returns the remainder when you divide the first value by the second. I don’t know what you mean by the first value and the second value. Are you saying “position()’ is the first, mod is the second. Or do you mean 2 (first value) divided by 1 (second value). 2 divided by 1 = 2 so that’s an even number. Please explain. Thanks.

  4. beargal:

    Modulus is a mathematical function which works like this: It returns the remainder when the first argument is divided by the second. So in our example:
    position() mod 2
    the current row’s index is divided by 2 and the remainder is returned. If the number is even, then the remainder is 0; if the number is odd, then the remainder is 1.

    You can use the modulus function (the notation in JavaScript is ‘mod’) with any two numbers:
    8 mod 3 = 2 (there are 2 3s in 8 with 2 left over)
    100 mod 10 = 0 (there are 10 10s in 100 with zero left over)
    3 mod 2 = 1 (there is one 2 in 3 with 1 left over)
    etc.

    Hope this clears it up for you.

    M.

  5. beargal says:

    Marc,
    Thank you for that explanation. Very helpful for someone new to this like me.

    ~beargal

  6. Nadeem Syed says:

    Marc,

    I am trying to display results from a SP list in multiple columns instead of one long column in a DVWP and can’t seem to find a solution. I have a list that contains site logos and their respective URLs and on the SP page I want to display these site logos in a grid view (multiple rows and columns) and when a user clicks on the logo they are taken to the respective site. I am able to pull the records from the list in a DVWP but the site logos are displaying in one single row and i want to divided them in multiple rows. The number of images will vary so I want the images to be displayed dynamically in 6 columns. Can you please help. Thanks.

    The code is below:

    .
    .
    .
    .

    <!– URL Path –>

    <!– –>

    <!—->

    ms-alternating

    There are no items to show in this view.

  7. Nadeem Syed says:

    Marc,

    I am trying to display results in multiple columns instead of one long column –using DVWP and SP list — the list contains images of site logos and their respective URLs — and I want to display these site logos in a grid (multiple rows and columns) – so that when users click on the site logo they are taken to their respective sites. I can’t seem to figure out how to divided the results to display in multiple columns (number of site logos is not a fixed value). Please help. Thanks.

    Here’s the code:

    .
    .
    .
    .

    <!– URL Path –>

    <!– –>

    <!—->

    ms-alternating

    There are no items to show in this view.

  8. Nadeem:

    Sorry for the delayed response. The trick here is to do something like this:

    </TR><TR>

    Basically, if the row’s position in the rowset is odd, we close off the current table row and start a new one. You can use any number you want in place of the 2 to set the number of columns. The reason we need to use the xsl:text tag is that SharePoint Designer will flag an error if we have an “uneven” set of TR tags.

    M.

  9. Erg. I hate it when I forget to set off the code in sourcecode tags…

    Nadeem:

    Sorry for the delayed response. The trick here is to do something like this:

    <xsl:if test="position() mod 2 = 1">
      <xsl:text disable-output-escaping="yes">&lt;/TR&gt;&lt;TR&gt;</xsl:text>
    </xsl:if>
    

    Basically, if the row’s position in the rowset is odd, we close off the current table row and start a new one. You can use any number you want in place of the 2 to set the number of columns. The reason we need to use the xsl:text tag is that SharePoint Designer will flag an error if we have an “uneven” set of TR tags.

    M.

  10. Nadeem Syed says:

    Marc,

    Thank you for your help on this. Your suggested code/trick works great.

    Once again thank you for providing such an excellent study and resource on DVWP with this “Unlocking the Mysteries ….” series. Hoping this series will continue…

    Nadeem


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!