Valid for Sitecore 5.3
Define XSLT rendering dynamically

Sitecore versions: tested with 5.3.0. Expected to work with 5.3.x releases.

The Problem

Define which XSLT rendering should be used on the page dynamically by using the XslTransform class.

1.  The Solution

Use XslTransform class to output the rendering on the page.

using System.Xml.Xsl;
using System.Xml.XPath;
using Sitecore.Data;

XslTransform transform
= new XslTransform();
XsltArgumentList args
= new XsltArgumentList();
//define the xslt rendering file
string xslFile = "http://localhost/xsl/test.xslt";
Database db
= Factory.GetDatabase("master");
//get the iterators for the root and context item
XPathNodeIterator iter = (new Sitecore.Xml.XPath.ItemNavigator(db.GetRootItem())).Select(".");
XPathNodeIterator contIter
= (new Sitecore.Xml.XPath.ItemNavigator(Sitecore.Context.Item)).Select(".");
            
//define and add the xslt extension classes
XslHelper sc = new XslHelper();
Sitecore.Web.UI.WebControls.ContentDot dot
= new Sitecore.Web.UI.WebControls.ContentDot();
args.AddExtensionObject(
"http://www.sitecore.net/sc", sc);
args.AddExtensionObject(
"http://www.sitecore.net/dot", dot);
//add parameters
args.AddParam("sc_item","",iter);
args.AddParam(
"sc_currentitem","",contIter);
args.AddParam(
"sc_test","","test");
//define the stream which will contain the result of xslt transformation
System.IO.FileStream stream = new System.IO.FileStream(@"c:\result.html", System.IO.FileMode.Append);
try
{
  
//load xslt rendering to XslTransform class
  transform.Load(xslFile);
  
//perform a transformation with the rendering
  transform.Transform(new Sitecore.Xml.XPath.ItemNavigator(Sitecore.Context.Item), args, stream);
  stream.Flush();
}
finally
{
  
// don’t forget to close stream
  stream.Close();
}