Developer

Practical application: Put XSLT 2.0 to work for you

XSLT 2.0 is no longer just a theoretical standard. With some readily available free tools you can put the power of XSLT 2.0 to work for you in your normal application development. Edmond Woychowsky shows which tools to get and how it all comes together in practice.

This article is also available as a TechRepublic download, which includes sample code listings and an example project exploring the potential of the methods shown.

Several weeks ago, I received an e-mail from a friend concerning part of an application I developed several years ago. The application in question had one of the most convoluted XSL style sheets I had developed up to that point. In fact, I wrote an article on one of the particular problems that the style sheet in question had to deal with. Anyway, my friend said he was looking into upgrading the style sheet to take advantage of some of the new features available in XSLT 2.0, which got me thinking.

Surprisingly, for once, I wasn't thinking about the style sheet; instead, I was thinking about how to apply the style sheet. The reason for this is that the original application was written in C#, and right now a lot of developers are waiting for Microsoft to get around to bringing MSXML up to the latest standard, because standards being on paper count for nothing if it isn't backed up with some actual working examples now and then. Or if you happen work for the U.S. patent office, think of it this way: What good is issuing a patent for warp drive if nobody actually knows how to actually build it? So in the end, unless you sell vaporware to venture capitalists or patent things to stake out some technological territory, being an idea kind of person will only get you so far. So let's build something that actually works.

The ingredients

A few everyday, readily available items are necessary to construct a working example. First, you need a supply of dilithium crystals... uh, sorry, wrong working example. Take two. First you need Microsoft's Visual C# Express, for creating the .NET code. The XSLT processor, however, can't be obtained from Microsoft; it needs to come from another source, Altova. In case you're unfamiliar with Altova, they're the folks responsible for XMLSpy and a slew of other XML-type goodies.

Before you get out your checkbook, the particular software we need from Altova is a freebie, AltovaXML 2007, which consists of an XML Validator, an XSLT 1.0 Engine, an XSLT 2.0 Engine, and XQuery Engine. The part of AltovaXML 2007 we're interested in for this example is the XSLT 2.0 engine, which, unlike MSXML, will allow our programs to use XSLT 2.0.

Since installing Microsoft's C# Express Edition has been covered before, I'll instead cover the highlights of installing AltovaXML 2007. Installation is merely a matter of clicking on the installation executable and following the prompts. Figures A and B show some of the installation screens you will see.

Figure A

Installation process

Figure B

Another part of the installation process

Once you've installed AltovaXML 2007, using it is about as difficult as installing; just add a reference, as shown in Figure C.

Figure C

Adding a reference

Now that the preliminaries are out of the way, we're ready to create a C# application like the one shown in Figure D. Since this is merely a proof of concept, the XML shown in Listing A as well as the XSLT in Listing B are recycled from a previous article. This gives us the opportunity to concentrate on using AltovaXML 2007 in our program, shown in Listing C.

Listing A — The XML document

<?xml version="1.0" encoding="UTF-8"?>
<world>
<country name="Canada" continent="North America">
<city>Toronto</city>
<city>Vancouver</city>
</country>
<country name="Jamaica" continent="North America">
<city>Kingston</city>
<city>Ocho Rios</city>
</country>
<country name="United States" continent="North America">
<city>Allentown</city>
<city>Mobile</city>
</country>
<country name="United Kingdom" continent="Europe">
<city>London</city>
<city>Dundee</city>
</country>
<country name="France" continent="Europe">
<city>Paris</city>
<city>Nice</city>
</country>
<country name="Japan" continent="Asia">
<city>Tokyo</city>
<city>Osaka</city>
</country>
</world>

Listing B — The XSLT 2.0 style sheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:fn="http://www.w3.org/2005/xpath-functions"xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:template match="/">
<xsl:element name="world">
<xsl:for-each-group select="//country" group-by="@continent">
<xsl:sort select="@continent" data-type="text" order="ascending"/>
<xsl:variable name="continent" select="@continent"/>
<xsl:apply-templates select="//country[@continent = $continent]" mode="group">
<xsl:sort select="@name" data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
<xsl:template match="*" mode="group">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

Listing C — The C# program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}

privatevoid Form1_Load(object sender, EventArgs e)
{
AltovaXMLLib.ApplicationClass objAltova = new AltovaXMLLib.ApplicationClass();

textBox1.Text = Application.StartupPath + "\\..\\..\\world2.xml";
textBox2.Text = Application.StartupPath + "\\..\\..\\grouping2.xsl";
objAltova.XSLT2.InputXMLFileName = textBox1.Text;
objAltova.XSLT2.XSLFileName = textBox2.Text;

textBox3.Text = objAltova.XSLT2.ExecuteAndGetResultAsString();
}
}
}

The example

In the above code, some things will stand out right away, other than my hard-coding file names, like the AltovaXMLLib.ApplicationClass object, which is where all of the magic takes place. Because I'm currently only interested in the XSLT 2.0 features of AltovaXML 2007, Table A shows only the methods that apply to XSLT 2.0 and Table B shows only the applicable properties.

Table A — Methods

Method

Description

Execute

Apply the current XSL style sheet to the current XML and write the result to the file name provided.

AddExternalParameter

Add a parameter value to the XSL style sheet.

ClearExternalParameterList

Clear all previously added XSL style sheet parameters.

ExecuteAndGetResultAsString

Apply the current XSL style sheet to the current XML and return the result as a string.

Table B — Properties

Method

Description

InputXMLFileName

Set the file name of the XML document to transform.

InputXMLFromText

Set the XML document to transform from a string.

LastErrorMessage

The last error message as a string.

XSLFileName

Set the file name of the XSL style sheet.

XSLFromText

Set the XSL style sheet from a string.

XSLStackSize

The maximum depth of recursion allowed in the style sheet. The default value is 1000 and the minimum allowed value is 100.

The result

When all of the above is brought together, the result should be something that looks like Figure D.

Figure D

The result

Where to go from here

The purpose of this example was to show that XSLT 2.0 is more than an intellectual exercise; it is actually possible to use it today. While the example shown in this article was really plain vanilla, it gives you a hint as to what is possible using free resources. In an actual business environment, you might want to make a couple of purchases, like Michael Kay's XSLT 2.0 Programmer's Reference and a copy of Altova'sXMLSpy. Yeah, XMLSpy will set you back a few dollars, but the peace of mind in knowing that your style sheets will work the same in a test environment as they do in the actual environment will be worth it.

Editor's Picks

Free Newsletters, In your Inbox