I have a number of code libraries I have created or inherited. As time allows or urgency requires I am busily converting them into .NET 2.0 projects. For the most part this is straightforward, but every now and again I run into something interesting that requires a little more digging/coding/swearing.
Today I discovered the XslTransform
class has been deprecated in 2.0. Since I do a fair amount of work with XSLT I decided I should pay attention to the little compilier warning Visual Studio was kind enough to generate and see what I should be using instead. The new preferred class is XslCompiledTransform
and Microsoft has also generated handy guides for using the new beast
and to migrating your old XslTransform code
. Clearly there was a fairly meaty change made, so time to do some reading.
The documentation promises increased performance and also states "The new XSLT processor compiles the XSLT style sheet down to a common intermediate format, similar to what the common language runtime (CLR) does for other programming languages. Once the style sheet is compiled, it can be cached and reused." Very interesting. I will have to do some side-to-side comparison tests for performance at some point, but what do I have to do now to get my old code using XslCompiledTransform?
The function I need to change takes an XML document, calls another function that returns custom XSLT and then uses this to transform the input document and write it to a string representation of an XML document. The transformation simply sorts the document based on a number of supplied parameters. (There is a better way to do this, but I inherited this particular method and it "works". Also the need for the method will be removed shortly, so I just want to get it work in 2.0.)
Replacing the XslTransform class with XslCompiledTransfrom causes an error in the .Transform call that is the heart of the procedure...
Dim xTrans as XslCompiledTransform
'1.1 overload (IXPathNavigable, XsltArgument,TextWriter, XmlResolver) - Documentation
xTrans.Transform(InputXMLDoc, Nothing, XMLStringWriter, Nothing)
Looking at the overloads for XslCompiledTransform.Transform reveals that what I want to do is...
'2.0 overload (IXPathNavigable, XsltArgument,TextWriter)
xTrans.Transform(InputXMLDoc, Nothing, XMLStringWriter)
The code wasn't using the Resolver
anyway, which is only used if you need to reference external resources such other DTDs or stylesheet namespaces, which isn't the case here. If I wanted to use a resolver, I would need to use the one Transform overloads
(of 14) that supports it, which requires an XMLReader as an input and an XMLWriter as an output. Fortunately, I don't need to do that much rewriting. Dropping the unused XmlResolver parm does just what I need.