Click here to Skip to main content
Click here to Skip to main content

Debugging XML Transforms (XSLT) with Visual Studio

By , 26 Feb 2011
 

XSLT stands for Extensible Stylesheet Language Transformations.

[Update/Note] This post describes the XSLT debugger which is only available
in the Professional and Team System versions of Visual Studio.

There is so much to learn in our technical world… and so little time….and for some of us, even fewer brain cells. Alas, such is the life of a nerd/dweeb/geek.

The latest technology I spent brain cells on is XML Transforms. You've probably seen these mysterious little files with the XSLT extensions that do magically wondrous things to XML files.

For traditional developers, XSLT is weird because it's not a traditional procedural language: "Do A, if successful then do B". XSLT uses "templates" with XPATH to select a group of nodes and then formats the selected nodes in the template body. To delve deeper into the document, XSLT uses sub-templates in a recursive tree-like fashion. However, these sub-templates are disjointed hunks of text in the XSLT document so it's difficult to visually comprehend what is going on…at least to an untrained eye.

This post isn't going to explain how XSLT transforms work—it's going to explain how to run them under the Visual Studio Debugger and watch them do their magic. FYI: I only tried this with VS 2008.

For more information on XSLT, go to the WC3 website here.

Here is the XML input file we are going to work with:

<?xml version="1.0" standalone="yes"?> 
<Golfers> 
  <Golfer> 
    <ID>1</ID> 
    <Name>Bobby Jones</Name> 
    <Birthday>1902-03-17</Birthday> 
  </Golfer> 
  <Golfer> 
    <ID>2</ID> 
    <Name>Sam Snead</Name> 
    <Birthday>1912-05-27</Birthday> 
  </Golfer> 
  <Golfer> 
    <ID>3</ID> 
    <Name>Tiger Woods</Name> 
    <Birthday>1975-12-30</Birthday> 
  </Golfer>
</Golfers> 

Here is the XSLT file we are going to transform it with:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/Golfers"> 
  <Golfers> 
    <xsl:apply-templates select="Golfer"/> 
  </Golfers> 
</xsl:template> 
<xsl:template match="Golfer"> 
  <xsl:element name="Golfer"> 
    <xsl:attribute name="ID" > 
      <xsl:value-of select="ID"/> 
    </xsl:attribute> 
    <xsl:attribute name="Name" > 
      <xsl:value-of select="Name"/> 
    </xsl:attribute> 
    <xsl:attribute name="Birthday" > 
      <xsl:value-of select="Birthday"/> 
    </xsl:attribute> 
  </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

The transform file will turn the XML elements into attributes. Why? Because the .Net DataSet/DataTable class seems to prefer XML attributes when reading XML files. And, because after studying XML for a long time, I've come to prefer sub-elements when there is a one-to-many relationship and to prefer attributes when there one-to-one relationship between the elements.

To start, first open up the XSLT file in Visual Studio:

The XML tool bar should appear:

If it doesn't appear, right click on the toolbar area and turn it on:

Put a breakpoint in the XSLT file (F9 or use the mouse or use the Debug menu):

Click the Debug XSLT button:

The first time you run it, an open file dialog will prompt you: "Choose Input XML Document"

Note: Subsequently when you run it, the input file will open automatically*.

The standard Debug toolbar will appear. With the toolbar you can start stepping through the transform code. Two synchronized windows will be open:

In the image above, I've clicked 'step' several times. You can see in the right window that the second XML node is being processed.

If you go to the Debug menu and select Locals, you can get more information about what is happening:

You can do watches and other debugging things while the code is paused. You can see the output file being built in the window behind the input XML window.

When it's all done the new XML file will look like this:

<?xml version="1.0" encoding="utf-8"?> 
<Golfers> 
  <Golfer ID="1" Name="Bobby Jones" Birthday="1902-03-17" /> 
  <Golfer ID="2" Name="Sam Snead" Birthday="1912-05-27" /> 
  <Golfer ID="3" Name="Tiger Woods" Birthday="1975-12-30" /> 
</Golfers> 

As my Canadian friends would say: "That's pretty cool, eh?"

*To override the automatic opening of the XML input file, right click the XSLT file and select properties. You can edit the input and output file paths and names:

That's it. Who knew the Microsoft boys and girls would go to such depths to provide such a powerful tool. It was a pleasant discovery.

I hope someone finds this useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
Member
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberJ. Torrecillas21 Aug '12 - 0:47 
Simple but useful for first XSLT approach.
 
Thanks!!]
QuestionExcellent notes and illustrationsmemberab17798 Sep '11 - 9:12 
This is great! I was looking for help debugging my XSLT code. These written and illustrated steps are excellent. It all worked the first time. Simply Amazing!
GeneralMy vote of 3memberhemant000528 Feb '11 - 18:50 
Indeed a good discovery!
GeneralMy vote of 5memberL Hills25 Feb '11 - 3:38 
The last point about the properties. I was trying to figure out how to run the transform against a different source file and there is the answer. Thanks!
GeneralOutstanding walk-through, thanksmemberAre Jay14 Jan '10 - 18:34 
Steve,
 
I have an extensive skins library that uses xslt transformation that I help support and we are using xsl Extension objects within our xslt. I need to debug this code but an unable to step though it without loading our proprietary Extensions. These objects are loaded during the Transformation process within C#.
 
Is the a way to use Visual Studio's xsl debugger while initializing our Extensions?
 
Maybe:
- custom VS 2008 Plugin
- purchase VS 2008 Plugin
- purchase xsl debugger
- hard coded method of adding our Extensions to the xsl style sheets
 
Any help in pointing me in the right direction our be much appreciated!!
 
I'm listening but I only speak GEEK.

GeneralRe: Outstanding walk-through, thanksmemberSteve Wellens15 Jan '10 - 3:41 
Sorry,
 
I don't know off-hand. Good Luck!
 
Steve Wellens

QuestionGreat explanationmemberGreen_Ash31 Dec '09 - 11:34 
I am trying to debug an xslt file from an application. The application uses the following call:
 
xsltFile.Load(TemplateFilePath+ "\\AppSendImport.xslt");
 
When I am at this line, I hit F11 (I have already loaded the xslt file in VS). The code is executed without stopping at the breakpoint. Of course when I try to set the breakpoint, it says that the symbols are not loaded. So, I didn't expect the breakpoint to work. What do I need to do to load the symbols and make breakpoint work?
 
Thanks so much
 
Ash
AnswerRe: Great explanationmemberSteve Wellens31 Dec '09 - 11:47 
I don't think you can do that.
 
It's only for doing standalone debugging, not from inside another debugger.
 
Steve Wellens

GeneralMy vote of 1memberwinheart26 Oct '09 - 1:02 
Waste of time, by searching this xml editor tool.
QuestionNot able to find XML editor in tool bar context menu.memberwinheart26 Oct '09 - 1:01 
Hi,
 
Are you using third party tool or any additional installation required for getting that menu.
 
Please don't post these kind of articles with out proper usage of all type of visual studio versions.
 
Winheart Sniff | :^)
AnswerRe: Not able to find XML editor in tool bar context menu. [modified]memberSteve Wellens26 Oct '09 - 4:00 
There is no third party tool.
 
The XML/XSLT debugging features are not available in the Express editions of Visual Studio.
 
http://msdn.microsoft.com/en-ca/vstudio/products/cc149003.aspx#divGeneral[^]
 
Steve Wellens
modified on Tuesday, October 27, 2009 12:10 AM

GeneralGreat 'hidden' featurememberMember 89362125 Oct '09 - 23:05 
Heh, after years of developing in that environment you would think you've seen it all. Then comes a surprise Smile | :)
I had no clue about this but wished I'd had that every time I worked with XSLTs. Thanks!
QuestionGreat, who knew?memberRick Hansen8 Sep '09 - 5:11 
Thanks,
 
I never knew this tool existed in VS2008. I've been doing my XSL/FO transforms (to PDF reports) by trial and error. At least now I can debug them sanely...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 26 Feb 2011
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid