0

I am working on a data view web part in SP 2010 to rullup some information from sub sites. These sites contain project task lists that have tasks assigned to users. The customer wants to see a rollup that will list projects by user based on whether or not they have tasks assigned. Since a project can have multiple tasks, a project could show up under multiple users in the rollup, depending on which tasks are assigned to which users.

Long story short, I need a way to select the count of distinct projects for each user.

The xml structure is like this:

<Rows>
  <Row Project="Project 1" TaskID="1" AssignedTo="Worker A" />
  <Row Project="Project 1" TaskID="2" AssignedTo="Worker B" />
</Rows>

From this, I would expect the following for project counts:
Worker A: 1
Worker B: 1

I am trying the following formula:

count($nodeset[not(@Project=preceding-sibling::Row/@Project) and @AssignedTo = current()/@AssignedTo])

This returns a count of 1 project for Worker A, but it returns 0 for Worker B, because Project 1 has already been counted for Worker A.

I don't know much about xslt, but from what I understand, using current() is not the most resource-friendly method. I've tried to do the muenchian grouping but I haven't been able to get it to work. Willing to try again though. Any advice would be helpful, as, again, I stink with xslt.

gsmith140
  • 53
  • 1
  • 8
  • So, what should be the expected output from this XML document? Please, edit the question and provide this important information. Do you only want the count (exactly of what?) or do you want all tasks assigned to a given user? – Dimitre Novatchev Aug 22 '12 at 12:20
  • I apologize, that was a little vague. I just need a count of distinct projects for each user, even if the project repeats between users. I updated the post. – gsmith140 Aug 22 '12 at 12:45

1 Answers1

0

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kWorker" match="@AssignedTo" use="."/>
 <xsl:key name="kProjByWorker" match="@Project" use="../@AssignedTo"/>
 <xsl:key name="kProjWorker" match="@Project" use="concat(.,'+',../@AssignedTo)"/>

 <xsl:template match="/*">
  <xsl:apply-templates select=
  "//@AssignedTo
     [generate-id()
     =
      generate-id(key('kWorker',.)[1])
      ]"/>
 </xsl:template>

 <xsl:template match="@AssignedTo">
  <xsl:value-of select="concat('&#xA;',.,': ')"/>

  <xsl:value-of select=
   "count(
     key('kProjByWorker', .)
       [generate-id()
       =
        generate-id(key('kProjWorker', concat(.,'+',current()))[1])]
     )"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document (a more challenging version of the provided one):

<Rows>
  <Row Project="Project 1" TaskID="1" AssignedTo="Worker A" />
  <Row Project="Project 1" TaskID="2" AssignedTo="Worker B" />
  <Row Project="Project 1" TaskID="2" AssignedTo="Worker A" />
  <Row Project="Project 2" TaskID="1" AssignedTo="Worker A" />
</Rows>

produces the wanted, correct result:

Worker A: 2
Worker B: 1

Explanation:

Use of the Muenchian grouping method twice: to find all different workers, and then to find the distinct projects assigned to this worker.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I tried this with my straight xml output, and it worked correctly. When I try to integrate it into the existing xsl stylesheet that is part of the data view web part, I just get 2 for every user. I'm trying to select that count in a template that already exists. I'll keep playing around with it. – gsmith140 Aug 22 '12 at 15:22
  • @gsmith140, Your current question is completely answered. Please, *accept* this answer (by clicking on the check-mark next to the answer). Please, describe any new problems you have in new question(s). – Dimitre Novatchev Aug 22 '12 at 16:02
  • I will accept it as the answer, but I still don't have a working solution, so I will probably post a follow-up question. – gsmith140 Aug 22 '12 at 16:44
  • @gsmith140, You are welcome. Please, let me know via a comment when the new question is posted. – Dimitre Novatchev Aug 22 '12 at 16:51
  • @DimitreNovatchev, quick question: why do you use both the `kProjByWorker` and `kProjWorker` keys? Can counting unique projects by worker not be done with one key? – ABach Aug 22 '12 at 18:54
  • @ABach, If you show the one key with which this can be done, then I can answer this question. I am using the keys in this answer both for efficiency and expressiveness. – Dimitre Novatchev Aug 22 '12 at 20:00
  • @DimitreNovatchev - never mind. Through some experimentation, I see why you use two keys; trying to do things via one key ends up being more trouble than it is worth. – ABach Aug 22 '12 at 20:55