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

Technorati Tags: ,,