Tuesday, June 23, 2009

How to test XSL transformations

Summary: Brief overview of several tools that can be used for testing XSL transformations.
UPDATE: If you program in Visual Studio, you don't need any additional software for testing XSL transformations. Visual Studio allows you to test and debug XSLT better than any other tool I tried. If you don't use Visual Studio, please read the rest of this post.
I recently spent several days redesigning XSL templates that our application uses to generate email messages. To get the job done faster, I needed a FREE tool that would help me edit XML files and test XSL transformations. [Note: I'm not against commercial software, but I did not want to get into the licensing mess, since other team members would also need to use the tool.] Microsoft's recommendation to create an ASP page for testing XSL transformations seemed like an overkill, so I looked elsewhere.

Had I checked StackOverflow earlier, I would've learned that Visual Studio 2008 offered all features I was looking for. Alas, I trusted Google more, so I started by trying out the tools that I found first:This is how these tools stacked up for me.

XML Notepad 2007 is a free utility offered by Microsoft. To preview XSL transformations in XML Notepad 2007, you first need to open the XML data file.


Then switch to the XSL Output tab, specify the XSLT file, and click the Transform button.


Although, XML Notepad 2007 is great for previewing XSL transformations, it is not very usable for editing XML documents. The editor does not seem to offer an option to edit the source and I found the built-in XML-specific editing features awkward to use. Another limitation of XML Notepad 2007 is that it does not allow opening more than one XML file at a time. It also considers the XML file to be the primary file for the transformation (other tools let you specify the XSL file for the XML file, as well as the XML file for the XSL file).

XML Copy Editor has been recently updated. I tried version 1.2.0.4 which was released in April 2009.


XML Copy Editor lets you edit the source of XML/XSL files, but generating previews takes a couple of steps. Once you load the data XML file, select the XML - XSL Transform menu option, and the tool will prompt you to enter the location of the XSLT file. After you specify the XSLT file, XML Copy Editor generates a temporary output file, which you can preview in an external browser by clicking the Browser toolbar button. I did not like this multi-step operation and the need to switch between two applications, so I moved on.

WMHelp XMLPad (v. 3.0.2.1) turned out to be the tool I used most. At first, I could not figure out how to automatically generate HTML preview after transformations (the tool kept opening the source of the transformed XML file). Eventually I determined that I needed to include the <xsl:output method="html"/> element in the XSL file and preview started to work.


To generate the preview, you need to specify the XML file for the open XSL file (there are a few gotchas here), and then simply click the Debug current stylesheet toolbar button.


Once the preview tab appears, you can switch between the Source and Preview modes. The tool can also open the preview page in an external browser, but I found the internal preview option more useful. WMHelp XMLPad keeps your options (such as mappings between XSL templates and XML files) in the styledata.xml file in the same folder as the XSL files. Although I found WMHelp XMLPad useful (which was before I discovered the capabilities of Visual Studio), it has a decent number of annoyances including the following:
  • Help page does not work; causes error "Not Found - The requested URL /doc/XmlPad/Welcome.htm was not found on this server."
  • Text editing features are really crippled. Tabs are not predictable: sometimes pressing Tab moves cursor by 8 positions, sometimes it moves by one position; you never know. If you select a block of text and press Tab, the selected text will be deleted. Shift+Tab (moving selected block to the left) obviously does not work.
  • The editor lets you place cursor anywhere. For example, you a line is 10 characters long, you can click on position 25 (a typical text editor would not allow this), hit Enter, and the editor will insert 15 new space characters at the end of the line.
  • For some reason the editor leaves a number of blank lines at the end of the document.
  • I already mentioned the need to define the output method in XSL files to auto-generate HTML previews.
  • Auto-complete issues. Say you are entering an <a> tag in front of the word link. The auto-complete feature will turn your text into the following: <a></alink>link. Also, auto-complete uses the Enter key as a selector; pressing Tab will just insert a tab character (or whatever space characters the editor decides to use).
  • Pressing the Home key moves the cursor to the beginning of line instead of the first character of text.
  • The File - Open option does not allow opening multiple files (although, you can drag and drop multiple files from Windows Explorer and the editor opens them fine).
  • You cannot perform searches across multiple files.
  • A highlighted word does not get automatically entered in the Search dialog, so you need to type it in.
  • To see transformation properties (such as XML source file), the cursor must be moved to the top of the XSL file. The properties also gets disabled if you open a read-only file.
  • Right-click clears text selection, so you can't copy or cut selected text using a context menu.
  • Debug toolbar occasionally disappears.
  • Switching between open files showing transformation previews causes losing the previews.
  • Changing the source XML file for a template, results in the preview tab displaying XML text instead of the preview.
  • It would be nice to be able to open a new tab (with an empty document) by double-clicking empty space in the tab area (a'la Firefox).
  • There are occasional errors (for the most part, the errors are not serious, although once I had to restart the program).
Despite all these annoyances, I found WMHelp XMLPad quite useful.

Xselerator was the last free XML editor I tried and I did not like it at all. First, it does not have an installer, so I had to go through the motions of creating folders, extracting files, defining shortcuts. Having done this I started the program and immediately encountered an error: "CoCreate failure on IAltovaApplication. Error: Class not registered." Apparently, the tool expects the AltovaXML engine to be installed. It would be nice if there were an indication of this requirement somewhere (installer?). The error does not seem to affect functionality, but still... Anyway, some people praise Xselerator, so you may want to give it a try.

If I were to test XSL transformations in future, I would stick with Visual Studio, but if you're looking for a free alternative, see if any of the applications I described fits your needs.

Monday, June 15, 2009

My.Utilities Class Library

Summary: Helper utilities library that some .NET developers may appreciate.

For several years, I have been compiling a library of utility/helper classes reused across multiple projects. You may recognize some of these classes if you read my published articles, but there are several classes that have not been released to the public, yet.

Here is a brief description of the most useful classes in the library (and there are more):
  • BaseForm
    I derive all my web form classes from this class. This classed extends the functionality of System.Web.UI.Page by offering many handy features, such as easy time localization (automatically converts time from GMT/UTC to local time on the client and vise versa; for additional information read It's About Time), invoking JavaScript pop-ups from code-behind, quick enabling/disabling/showing/hiding page controls, setting/getting session parameter in/from a cookie, retrieval of data values from form/query string, deep search for controls, and more.
  • AppProcess
    Loads a named assembly from a non-standard path (assuming that you specify alternate paths).
  • ExceptionInfo
    Generates formatted error messages for different type of exceptions based on various parameters and exception types, retrieves messages from inner exceptions and database error objects (for additional info, read Dissecting Error Messages).
  • Helper
    Okay, I agree: this is not a good name... Anyway, this class makes it easy to check for empty objects of different data types. For example, you can check for empty or null array in a single statement (if (Helper.IsEmpty(myArray)) instead of having to check for null first, and then for the number of elements in the array.
  • WindowsService
    Simplifies implementation of Windows services. Other classes in the same namespace (My.Utilities.Services) make it trivial to write periodic tasks (daily, weekly, etc) that Windows services normally perform (for additional info, see Implementing Windows services in Visual Studio 2008).
  • DataHelper
    Converts database values to standard .NET types, so you do not have to worry about special handling of DBNull values.
  • Name
    Makes formatting of people's names based on the available name parts easier, e.g. John F Kennedy, Jr; Kennedy, John F, Jr; John F Kennedy; Kennedy, John F; John Kennedy; Kennedy, John; etc.
To see the description of all classes in the library, download the help file:
My.Utilities.chm
This help file covers all classes, methods, properties, etc., and even offers a few samples.

If you are still interested, you can download the library project:
My.Utilities Class Library Project (Visual Studio 2008)
Just keep in mind that I started writing this library around 2001/2002, so some functionality could've been improved had the technology -- nullable types, generics, etc. -- been available at the time. Also, the general disclaimer applies: use at your own risk.

I hope this helps someone, but if you run into any issues, please let me know.