Supporters of End User
Web

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

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories:MOSS; WSS; 2007; 2010; Unlocking the Mysteries of the Data View Web Part; Site Manager/Power User; SharePoint Designer; Data View Web Part; Templates

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

Comments

Emmanuel

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

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 ?

Posted 23-Apr-2010 by Emmanuel
Marc Anderson

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

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.

Posted 23-Apr-2010 by Marc Anderson
Craig

dvt_1.rowview Template

Maybe I missed somethign. Doesn't the template dvt_1.rowview need to have a paramater declared to receive a row?

Posted 13-Nov-2012 by Craig

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: