Confession time – I love XSLT.
Most will probably find this rather strange but as a non-developer I find it gives me so much flexibility to present information from lists or from other sources for that matter.
If you are new to XSLT then you probably find it rather daunting and may not even be clear why it is useful to you. I am going to try to write a short series of blogs for those who would like to learn more about XSLT and what you can do with it.
XSLT is contained in a StyleSheet. If you use SharePoint Designer than it will recognise an XSL file extension and provide you with some nice intellisense and formatting – but essentially it is just a text file. It has a collection of ‘templates’ which get the data from the list and present it on the page using HTML.
In SharePoint, XSLT is becoming increasingly important given that the new list web parts are XSLT based. XSLT can be used to format the output from all of the following:
- List View web parts
- Data View web parts
- Content Query web parts
- Search web parts
You can either modify the XSLT directly in the page itself or create a separate XSLT stylesheet file and then link it to any web parts you would like to use it with. The advantage of the external XSLT file it that you can apply it to publishing pages and reuse it on several web parts on different sites and pages.
For the purposes of this example I am going to use a very simple SharePoint list with the standard Title column and one other column called URL which is set to be a Hyperlink.
Title = Single line of Text
URL = Hyperlink
The following section of code includes comments to explain what is going on and can be pasted into a text file and saved as tabular.xsl.
This section is the set up and can be used at the top of any external XSLT stylesheet file
exclude-result-prefixes="xsl msxsl ddwrt"
<xsl:output method="html" indent="no"/>
End of Set Up
The initial template which in this case is matching everything with "/"
It then creates a variable called Rows - this is accessed as $Rows
A standard HTML table and header row with the names of our columns is next followed by a loop through each row of the list and calls our second template dvt1-rowview to display the contents
<xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema">
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />
<xsl:call-template name="dvt_1.rowview" />
End of first template
Standard HTML rows and cells contain the contents of our list row
xsl:value-of command is used to display our columns
Columns are accessed as @InternalColumnName
<td><xsl:value-of select="@Title" /></td>
Now edit a page in SharePoint and add your list with the Title and URL columns onto the page.
Edit the web part and open up the Miscellaneous section. Type in the name of the saved XSL file including a path if needed.
Apply the changes and you should now see the results looking something like this:
This is very simplistic and just gives us a tabular output from our list very similar to what you get from a standard List View. Next post we will build on this further to see what more we can do with XSLT in SharePoint.