Skip to main content

Cloud

XSL to convert DataSet to HTML table

I was recently creating a web part for a WSS installation that needed to connect to a backend data store, run a query based on the user’s context in SharePoint, then display the results in the UI.
It was an interesting situation, as I didn’t have any developer access to the backend data store, nor did I have a good knowledge of the database schema.
To help troubleshoot, I created a generic XSL stylesheet that will transform the XML output of any .NET DataSet into an HTML table. This allowed me to get through my project much more easily, as it turned my webpart into a poor-man’s query tool.
Maybe this will help you out: if not, at least I know where to find it next time. 🙂
As shown, the XSL will only display a single table within a DataSet; you could pretty easily modify it to iterate through a table collection, though.
Here’s the XSL:

<?xml version="1.0"?>
<
xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0">

<xsl:output method="html" version="4.0"/>

<xsl:template match="NewDataSet">
<
div>
<
table border="1">
<
xsl:for-each select="//Table" >
<
xsl:call-template name="DoLine" />
</
xsl:for-each>
</
table>
</
div>
</
xsl:template>

<xsl:template name="DoLine" >
<
xsl:if test="position()=1">
<
tr class="header" >
<
xsl:for-each select="./*">
<
td>
<
xsl:value-of select="local-name()" />
</
td>
</
xsl:for-each>
</
tr>
</
xsl:if>

<tr class="row" >
<
xsl:for-each select="./*">
<
td>
<
xsl:value-of select="." />
</
td>
</
xsl:for-each>
</
tr>

</xsl:template>

</xsl:stylesheet>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Matthew Morse

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram