1,739 articles and 13,478 comments as of Sunday, October 24th, 2010

Thursday, February 11, 2010

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 7: xsl:for-each

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

<xsl:for-each>

A way to iterate over a nodeset (group of rows).

Once you have a rowset like the $Rows I talked about in the prior article about <xsl:variable>, you’ll want to do something with it.  If you’ve ever used a report writer or created reports in an application like Microsoft Access, the basic constructs here may look a little familiar.  Basically, you will want to have some sort of header and/or footer information (perhaps at multiple grouping levels) and then you’ll want to show some information from the detail rows.  What you show in the detail rows generally comes from the items in the rowset, which we’ve defined as $Rows.  (I’ll forego carrying forward my $Bob or $Strawberry_Jam examples to avoid confusion.)

Once again, let’s look at the basic example which I’ve been using in this series.  (I keep including it just to make it simpler than flipping back and forth.  I’m not fond of flipping back and forth.)

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

In the dvt_1 template, we set up the rowset variable, $Rows, and start outputting the TABLE which will contain the DVWP output.  The next thing within the TABLE is a table row (TR) which contains a single table header cell (TH)  with the text “Title”, which will be the header for the Title column. (Ignore the $dvt_1_automode stuff for now.  I’ll get to things like that in a post at the end of this series.)

Next we call the dvt_1.body template.  As I’ve mentioned before, the dvt_1.body template is generally where we’ll output summary information about the rowset, either before or after displaying the detail rows, depending on our requirements.  Here we aren’t doing anything at the summary level, but we are calling dvt_1.rowview within an <xsl:for-each>.

What this does for us is to iterate over the set of rows, effectively calling dvt_1.rowview once for each list item in $Rows:

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

<xsl:for-each> only needs to have the select value, which should represent an XSL nodeset, or what I’ve been calling a rowset. (Yes, I probably use too many of these terms interchangeably. It comes from working with too many languages over the years and at the same time.)

Inside the <xsl:for-each> is also where you do any sorting with the <xsl:sort> tag.  (You guessed it, that’s for the next article.)

Next up: <xsl:sort>

Used within an <xsl:for-each> to determine the sort order in which the nodeset (group of rows) are processed.

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

2 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 7: xsl:for-each”
  1. Emmanuel says:

    Marc, Thanks for these series. Very useful. I have a list as such
    Title Section List
    item1 sectionA ListA
    item2 sectionA ListA
    Item3 sectionB ListB
    Item4 sectionB ListB
    item5 sectionB ListC
    item6 sectionB ListC

    I would like to customize the dataview to display this way:
    SectionA
    ListA : item1 | item2
    SectionB
    ListB : item3 | item4
    ListC : item5 | item6

    Any suggestion ?

    • Emmanuel:

      First, you’d want to sort your rowset by Section, then by List. Next, you’d want to use the ddwrt:NameChanged function in the rowview template to decide when to output the “headers” for Section and List. Finally, you’d set up the XSL in the rowview template to output the Title values with the | separator.

      Hope this helps.

      FYI – A question like this that is only sort of tangentially related to the actual post is probably better posted to Stump the Panel (STP) — link above.

      M.


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!