How to check if XslCompiledTransform instance has already loaded a stylesheet

| 3 Comments | No TrackBacks

XslCompiledTransform provides amazing transformation performance, but obviously not without a price. As with any compiling system the price is slower and quite resource consuming compilation stage. That's why it's very important to cache loaded XslCompiledTransform instance to avoid paying compilation price over again. One question that comes sometimes while implementing caching/pooling systems for XslCompiledTransform is how to check if XslCompiledTransform instance has been already loaded with a stylesheet?

XslCompiledTransform design separates object construction and initialization for the sake of reusability (to be able to load another XSLT stylesheet into the same XslCompiledTransform instance). I believe that's a legacy issue as it contradicts with always-cache-loaded-XslCompiledTransform mantra. And unfortunately currently XslCompiledTransform provides no property to check if the stylesheet was already loaded and XslCompiledTransform instance is ready to be used. Hopefully in the next version we will get such one. But currently here is a trick that can be used - check OutputSettings property for not null. It's always null when Load() method has not been called and always not null after that. This is pretty much safe and I was assured by guys at the Microsoft XML team responsible for the XslCompiledTransform that this behaviour won't be changed.

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/576

3 Comments

HOw to implement caching system for XslCompiledTransform?

Well, you are right, this really sounds like a weird problem. Should admit I was too busy to understand what exactly the problem is...

The original question was like this: "Is there a way to determine if an instance of the XslCompiledTransform class
has already loaded a style sheet? I am caching compiled style sheets and
storing them in a class. Alternatively, the private variable in the class
containing the style sheet might load it from a disk file. I would like to
test if a style sheet is present in the private variable but do not see a
way to obtain this information. I can always keep a parallel variable
indicating this is the case but wondered if there was a way or if anyone
else thinks this would be useful."

Oleg, I'm puzzled. Why would one want to reuse an XslCompiledTransform instance while loading it with a new stylesheet ? The cost of "new XslTransform" (the constructor is empty !) is so ridiculous compared to the price of compilation that it's not even funny.
Can you elaborate on why you think it better to rely on an undocumented behavior of the OutputSettings property rather than simply not reusing XslCompiledTransform instances for several stylesheets ?
Cheers,
--Jonathan

Leave a comment