Wednesday, June 01, 2011

Grouping list data in XSLT

I used a linked XSL file to override the output of my list web part, grouped by one of my custom columns called 'Classification'.

To add in an XSL override link to your XSL file via the list web part properties:


XSL (uploaded to a SharePoint Document library):

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office">
<xsl:include href="/_layouts/xsl/main.xsl"/>
<xsl:include href="/_layouts/xsl/internal.xsl"/>
<xsl:output method="html" indent="yes" version="4.0"/>
<xsl:template match="/">
<xsl:call-template name="MyTemplate" />
</xsl:template>
<xsl:key name="classificationKey" match="Row" use="@Classification" />
<xsl:key name="associatedTitle" match="Row" use="@Title" />
<xsl:template name="MyTemplate">
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('classificationKey', @Classification))]">
<h3><xsl:value-of select="@Classification" /></h3>
<xsl:variable name="thisClassification" select="@Classification" />
<xsl:for-each select="../Row[generate-id()=generate-id(key('associatedTitle', @Title)[@Classification = $thisClassification][1])]">
<div>
<strong>Title: </strong>
<xsl:value-of select="@Title" /><br/>
<strong>Description: </strong>
<xsl:value-of select="@Description" disable-output-escaping="yes" /><br/>
<strong>Link: </strong>
<a href="{@Link}" title="{@Title}"><xsl:value-of select="@Link"/></a>
</div>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This is the output:

Classification 1

Title 1
Description 1
Title 2
Description 2
Title 3
Description 3

Classification 2

Title 4
Description 4
Title 5
Description 5

No comments:

Post a Comment