April 17, 2005

XslCompiledTransform (new XSLT 1.0 processor in .NET 2.0) - no more pull-mode XSLT

I'm studying new XSLT 1.0 implementation provided by Microsoft in the .NET 2.0 Beta2 - XslCompiledTransform class. The guys who wrote it are my good friends and excellent developers, but let me to complain a little bit, not because I'm a complainer, but trying to make this cool piece of ...

XslTransform class, now obsoleted in .NET 2.0 has a very nice feature - it allows to perform pull-mode XSL transformations. The result of the transformation can be an XmlReader object and the actual transformation occurs only when you call that reader methods. That feature, traditionally overlooked and rarely mentioned (come on, who cares about perf nowadays, right?) enables very efficient XML pipelining with no temporary XML stores. If a component (such as SqlXml class) accepts XML as an instance of XmlReader and you need to feed it with XSLT output that is easy and efficient. I was trying to promote this feature with XmlTransformingReader class, which you can find in the Mvp.Xml library.

Now, new and shiny XslCompiledTransform class doesn't support this feature anymore, transformation output is now only Stream, TextWriter or XmlWriter, so it's pure push-mode transformer. That actually simplified API a bit, but now in a scenario when you need a transformation result as an XmlReader one needs 1) a temporary XML storage and 2) write nasty complicated code like this:

// Load XSL transformation
XslCompiledTransform xform = new XslCompiledTransform();
xform.Load (xslPath);
// Execute/Cache results
XmlDocument resultsDoc = new XmlDocument();
XPathNavigator resultsNav = resultsDoc.CreateNavigator();
// using makes sure that we flush the writer at the end
using (XmlWriter writer = resultsNav.AppendChild()) 
  xform.Transform(XmlData.CreateReader(), writer);
// Return results
SqlXml retSqlXml = new SqlXml (resultsNav.ReadSubtree());
return (retSqlXml);
It's from Michael Rys's excellent webcast on "Managing XML Data on the Database with SQL Server 2005 and Visual Studio 2005". Temporary XML document, what can be worse? In .NET 1.X this could be done much easier and efficient:
// Load XSL transformation
XslTransform xform = new XslTransform();
xform.Load (xslPath);

//Load source XML
XPathDocument doc = new XPathDocument(XmlData.CreateReader());

// Return results
SqlXml retSqlXml = new SqlXml (
  xform.Transform(doc, null, new XmlUrlResolver()));
return (retSqlXml);

Now that's a pity .NET 2.0 doesn't add any new XSLT-related functionality, but even cuts off some :( I wonder if this was cut because it was unfeasible to implement with new XslCompiledTransform architecture or the reason was a different one. Oh well, I filed this issue as a bug. Go vote for it if you care.