Top Contributors From a document Library using xslt

Requirement: Display the top contributors / authors in a document library with no server-side code but using xslt.

mysort

I searched all over the web and asked in forums but I couldn’t find something close to what I would like to achieve  so when I figured it out I decided that this needed to be posted.  So here is how I achieved my requirement:

Approach:

What has to be done basically is to get all distinct / unique authors from the document library and get the total count for each then you can start sorting and trimming results.

Using a DVWP we will customize the xslt to achieve our requirement.

xsl

Design Tab > Customize XSLT > Customize Entire View 

First we need to get the unique authors list. Then use count( ) to get the total for each unique author.


<xsl:variable name="unique-authors" select="$AllRows[not(./@Author.title=following-sibling::Row/@Author.title)]" />

<xsl:for-each select="$unique-authors">
<xsl:variable name="thisNode" select="."></xsl:variable>
<xsl:value-of select="@Author.title" />
<xsl:value-of select="count($AllRows[./@Author.title = $thisNode/@Author.title])"/>
</xsl:for-each>

So where halfway done here. Now we need to change our code above to get it to sort descendingly. We added them inside a variable named “total-sorted” that will contain item node to store data we want for sorting.


<xsl:variable name="unique-authors" select="$AllRows[not(./@Author.title=following-sibling::Row/@Author.title)]" />

<xsl:variable name="total-sorted">
<xsl:for-each select="$unique-authors">
<xsl:variable name="thisNode" select="."></xsl:variable>
<item id="{generate-id(.)}">
<xsl:attribute name="author">
<xsl:value-of select="$thisNode/@Author.title"/>
</xsl:attribute>
<xsl:attribute name="numOfContribution">
<xsl:value-of select="count($AllRows[./@Author.title=$thisNode/@Author.title])"/>
</xsl:attribute>
</item>
</xsl:for-each>
</xsl:variable>

The sorting part…

<table>
<thead><td>Author</td><td>Contributions</td> </thead>
<xsl:for-each select="msxsl:node-set($total-sorted)/item">

<xsl:sort select="@numOfContribution" order="descending" data-type="number"/>

<tr>
<td>
<xsl:value-of select="@author"/>

</td><td>
<xsl:value-of select="concat('(',@numOfContribution,')')" />

</td>
</tr>

</xsl:for-each>
</table>

Insert some if conditions for formatting…

For alternating rows:

<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">ms-alternating</xsl:attribute>
</xsl:if>

For limiting results(e.g. Display Top 5 authors only)

<xsl:if test="position() &lt; 6">
</xsl:if>

For Filtering results (e.g Category=’Best Practices’)

On top of your xsl stylesheet you will find this parameter AllRows which you can filter by column in our example is Category.
filter xslt

You can also use Sharepoint Designer for Filtering list which is a lot easier with it’s UI.