I just submitted a patch to the mvc contrib project that contains the xslt view engine I wrote for Xero without any of the dependencies from our own libraries or commercial components.
The bulk of the work is done in a class called XmlResponseBuilder which builds the xml document that is going to be transformed by the xsl stylesheet. Almost all the rest of the code is there just to build up this document.
The implementation of IView and IViewFactory were the easy bits
To use the View engine you need to take the following steps:
1. Get the MVC Contrib project from google code
repository url: http://mvccontrib.googlecode.com/svn/trunk/
2. If the patch hasn’t been applied yet you can download it from: http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=MVCContrib&DownloadId=23748
After downloading the patch you can apply it to the downloaded mvc contrib project.
3. I use a base controller to ensure that the correct data is passed to the view factory. This is the base controller class:
public abstract class XeroControllerBase : Controller
{
private readonly XsltViewData vData = new XsltViewData();
protected IHttpSessionState Session
{
get { return ControllerContext.HttpContext.Session; }
}
public XeroControllerBase()
{
ViewFactory = new XsltViewFactory();
}
#region Add datasource methods
protected void AddDataSource(IXmlConvertible dataSource)
{
vData.DataSources.Add(new XslDataSource(dataSource));
}
protected void AddDataSource(IXmlConvertible dataSource, string rootName)
{
vData.DataSources.Add(new XslDataSource(rootName, dataSource));
}
#endregion
#region Add variable methods
protected void AddPageVar(string key, string value)
{
vData.PageVars.Add(key, value);
}
#endregion
#region Add message methods
protected void AddErrorMessage(string message)
{
vData.Messages.Add(new ErrorMessage(message));
}
protected void AddErrormessage(string message, string controlID)
{
vData.Messages.Add(new ErrorMessage(message, controlID));
}
protected void AddInfoMessage(string message)
{
vData.Messages.Add(new InfoMessage(message));
}
protected void AddInfoMessage(string message, string controlID)
{
vData.Messages.Add(new InfoMessage(message, controlID));
}
protected void AddAlertMessage(string message)
{
vData.Messages.Add(new AlertMessage(message));
}
protected void AddAlertMessage(string message, string controlID)
{
vData.Messages.Add(new AlertMessage(message, controlID));
}
protected void AddInfoHtmlMessage(string message)
{
vData.Messages.Add(new InfoHtmlMessage(message));
}
protected void AddInfoHtmlMessage(string message, string controlID)
{
vData.Messages.Add(new InfoHtmlMessage(message, controlID));
}
protected void AddErrorHtmlMessage(string message)
{
vData.Messages.Add(new ErrorHtmlMessage(message));
}
protected void AddErrorHtmlmessage(string message, string controlID)
{
vData.Messages.Add(new ErrorHtmlMessage(message, controlID));
}
protected void AddAlertHtmlMessage(string message)
{
vData.Messages.Add(new AlertHtmlMessage(message));
}
protected void AddAlertHtmlMessage(string message, string controlID)
{
vData.Messages.Add(new AlertHtmlMessage(message, controlID));
}
#endregion
#region RenderView method hides
protected new virtual void RenderView(string viewName)
{
RenderView(viewName, string.Empty, vData);
}
protected new void RenderView(string viewName, string masterName)
{
RenderView(viewName, string.Empty, vData);
}
protected new void RenderView(string viewName, object viewData)
{
RenderView(viewName, string.Empty, vData);
}
#endregion
}
December 19, 2007 at 15:43
Hi,
I’m a big fan of XSLT, I used it in classic ASP but when I moved to .NET lost touch with it.
Thanks to the MVC and the separation of concerns there is a chance again to move away from those ugly < % %> blocks.
I read some time ago that using code in XSLT had memory leaks issues, do you know about that? I think it is necesary to access Session and Application State, Membership, Roles etc.
December 19, 2007 at 23:43
As for using XSLT in ASP.NET, not sure why you stopped using it. The XSL support in .NET is very good and I’ve found it much easier to manipulate XML in general using the .NET libraries. As for getting away from the ugly brackets, funny you should say that. I’ve found I’m using them a lot more with the MVC model. I almost never used it in straight webforms with code behind, but 3.5 makes things so easy with inline < %foreach(...)%> for example.
Just a different set of experiences I suppose.
December 22, 2007 at 02:01
The link to the patch is dead. Is there another location where this can be reached? I use XSLT a lot with ASP.NET and this is precisely what I would like to see in ASP.NET MVC. Your controller class looks similar to my implementation of MVC I created while working on http://www.tuscanweb.com. In fact, if there is some way I could contribute I’d like to be of assistance.
December 23, 2007 at 07:43
Igor,
I created a new patch today with increased test coverage. As soon as it’s in the trunk of MVCContrib I’m sure you can contribute to that open source project.
Cheers
Ivan
January 9, 2008 at 14:11
Thanks for the submission!!!
March 10, 2010 at 18:08
Hi Ivan
I’m evaluating whether the XSLT view engine would be suitable for some work i’m doing. We’d like to use XSLT 2 however. Do you have any tips?
Thanks
March 10, 2010 at 19:30
AFAIK .NET has no support for XSLT 2.0 unfortunately