Jaimal Chohan

The Cached, Compiled, XSL Web Control

Posted by: jaimalchohan on: August 3, 2009

I’ve decided to kick this blog off again, after much time off, and for my first post … I’ll introduce my Cached, Compiled, XSL Web Control. 

Ever used the XSL control?  It looks something like this:

 <asp:Xml ID="xml1" DocumentSource="/MyData.xml" TransformSource="/MyStyle.xsl" />

I was working on a XML content driven project recently where I had to do many XML transforms on a page, and the above simly did not cut it, for the following reasons:

  1. No Caching – Everytime I want to perform a Transform, the control has to read my XSL from disk
  2. Due to the lack of caching, the XSL has to be compiled for each transform.
  3. Can’t pass paramaters via markup, they all have to be done via the code behind, which can be slighly more difficult to manage
  4. XML either from disk or via string, why do I have to ToString my XMLDocument/XDocument?

Mainly it was the lack of caching that annoyed me

My solution? come up with my own XsltControl

I started off with a basic XsltControl, wrapped a WebControl around it and added caching CompiledTransforms.  Extra support was added for adding paramaters via markup.

So, now, you I can write

<custom:XsltControl ID="MyTransform" runat="server">
            <custom:Argument Name="markup" Value="This text came from a paramater added via markup" />
            <custom:Argument Name="resource" meta:resourcekey="Resource" />
</custom:XsltControl>

The control also allows you to set the path of a xsl include directory which it will cache and add cache dependencies to the Xsl for, so when the Xsl changes, or an includes file changes the cached version is invalidated and the new version picked up on the next transform.

Code for XsltControl

XsltControl
Visual Studio 2008 Solution. [you'll have to rename the file to .zip]

Leave a Reply