XSLT is not a difficult technology to grasp but when you don’t have a decent IDE to back you up debugging can be long and painful and impair productivity.
In this article I’ll cover off the very basics and provide a few useful tips.
A Simple Example
Here's a simple example. I have a piece of XML that defines some static content:
I want to do is render this page as HTML. The XSLT looks like this:
After opening each of these in Visual Studio 2008, you’ll notice an “XML” top-level menu. Clicking this will give you the options of debugging the transformation or showing the output.
XML file selected
XSLT file selected
Selecting “Debug XSLT” will display an Open File dialog letting you select the other file to complete the transformation. For example, if you have an XML file selected then you will select an XSLT file for the transformation.
The output will be:
(The parameter value hasn’t been picked up in this instance, but I’ll get to that shortly.)
Along the way you’ll get many of the usual debugging aids you are used to; stepping and value inspection.
And that’s all there is to it, really.
Tip #1 XML or XSLT First?
Whether you start with the XML or the XSLT you end up with transformed XML. The IDE remembers the second file selected so you can repeat the transformation. Unless you close the first file you do not get a chance to reselect the second file to complete the transformation.
If you have one example of XML but several XSLTs then select each XSLT first. As you initiate each transformation for the first time select the same XML file. Each time you modify the XSLT or XML you can simply re-run the transformations.
In the opposite scenario when you have you have one XSLT and multiple XML examples select each XML and when given the option select the XSLT file.
Tip #2 Including XSLT
Your solution may end up using several XSLT files some of which may be dependent on others.
Be aware that Visual Studio resolves such dependencies without you having to do anything. When it comes to using these XSLTs in your application you will have to ensure that dependent XSLTs can get picked up.
Just for the record, use the XslCompiledTransform class and call the Transform method.
public void Transform(
You will have to derive a class from XmlResolver with appropriate logic to locate any additional XSLTs.
Tip #3 Parameters Workaround
When it comes to passing parameters to an XSLT you have a little more work to do.
Place a breakpoint at the start of the XSLT and start debugging. In your Locals window the parameter will show up letting you edit the value.
When the appropriate element is reached this will pick up your edited parameter value.
Tip #4 Ignore the times in the Output window
After running a transformation two times are shown in the Output window.
Ignore both of these. The load time isn’t really all that important and the execution time is misleading.
When you work with XSLT in the .NET Framework (2.0 and later) you'll be using the XslCompiledTransform class. This class compiles the XSLT into IL and then jitters it on its first run. Visual Studio repeats this process in its entirety each time. Your application however, will probably take advantage of the compiled nature of the class and all transformations thereafter will only take one or two milliseconds.
• Visual Studio 2008 has some lightweight, but effective support for developing XML and XSLT.
• Choosing whether to debug XML or XSLT can be an important choice for productivity.
• Parameters can be achieved.
• The final XSLT will transform faster in your application than in the IDE.
Intellisense for XSLT: As I was putting this post together I came across this article. Although I haven’t implemented this myself it looks good for productivity. I'll investigate further at some point, you may want to as well.
Profiling XSLT: If you are lucky enough to have the Developer Edition of Visual Studio 2008 there is a profiler add-in that you can add to Visual Studio. This article covers the profiler in detail. This is well worth pursuing if your solution has a lot of XSLT you think may be a bottleneck.
Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Microsoft Visual Studio webpage.
About the Author
Scott is a C# developer. He takes great pleasure in producing more business value for less code.
He’s always on the look-out for better and more elegant ways of doing things and uses these in any one of the dozen or so side-projects he has on the go. Genuinely appreciates being told better ways of doing things and actively seeks such criticism.
• http://objektivs.blogspot.com (technical)
• http://treehouseofterror.blogspot.com (personal)
• http://objektivs.com (a work in progress that isn’t working)
Other related posts:
The New Zealand ALM Conference 2011 (Application Life Cycle Management)
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008