Counting Subsets of Items in a Data View Web Part (DVWP)

I’ve posted about this application before. It helps my client manage their inventory of sports tickets which they can use with their clients and vendors.  They have a regulatory reporting requirement to maintain information about who uses the tickets and what their relationship is.

In this case, we wanted to be able to count the total number of tickets per event and the number that were available or reserved.  (This is not a highly sophisticated data model or application: we have one item in the Tickets list per ticket.)

When you want to count subset of records, you must provide the count function with a nodeset, which in this case is a collection of items. The code snippet below is part of the XSL from a DVWP which has an aggregate data source made up of the Events list and the Tickets list.  I’m passing the EventID into the XSL which deals with the Tickets list in line 2, and getting the rows in the tickets list which have that EventID in line 3.

In the Tickets.body template, I want to count the number of rows which meet each of the criteria.  The trick here is to take the full set of rows for the event and ‘skinny’ them down for each criteria.  I do this by specifying the filter for the criteria, creating the proper nodeset, as in line 18 for the available tickets and line 22 for the reserved tickets.

<xsl:template name="Tickets">
     <xsl:param name="EventID"/>
     <xsl:variable name="Rows" select="/dsQueryResponse/Tickets/Rows/Row&#91;
  @EventID = $EventID&#93;" />
    <xsl:call-template name="Tickets.body">
        <xsl:with-param name="Rows" select="$Rows" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="Tickets.body">
    <xsl:param name="Rows" />
   
<td class="ms-vb">
        <xsl:value-of select="count($Rows)" />
   </td>
   
<td class="ms-vb">
        <xsl:value-of select="count($Rows&#91;@TicketStatus = 'Available'&#93;)" />
   </td>
   
<td class="ms-vb">
        <xsl:value-of select="count($Rows&#91;@TicketStatus = 'Reserved'&#93;)" />
   </td>
</xsl:template>

Technorati Tags: ,,

2 Comments

  1. Marc,

    Thank you for this post. I have a list of news items which various authors submit. Our CIO now wants to see which author posted how many news items. So basically in the column 1 would be author name and second column would have count of news items this author published. I have been trying to get a DVWP to do this but having such a hard time. I tried following your above example but getting errors and its not working. Can you please help as its seems easy but i guess its not. Your help is much appreciated – thanks.

    Reply
    • Nadeem:

      Well, given my example, you can see it’s possible to do what you want. It’s hard for me to help your directly, but you should be able to tune this example to meet your needs.

      M.

      Reply

Have a thought or opinion?