Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Generate .NET Code With XSLT
Autogenerate classes that provide strongly typed access based on your database's XML Schema Definition.
by Kathleen Dollard

May 2003 Issue

Technology Toolbox: VB.NET, XML, XSLT

Extensible Stylesheet Language Transformations (XSLT) is a declarative language that defines a series of rules for how XML is processed. Most XSLT transformations convert XML to HTML, but you can also use XSLT to create any type of text output, including VB.NET and C# files. I'll show you how XSLT works to generate VB.NET code, and I'll offer some hints on performing XSLT processing in .NET. This approach offers you a way to generate code to your own specifications and update the generated code easily. Portions of your application then become resilient to changes in database structure or evolving requirements.

ADVERTISEMENT

As an example, I'll show you the XSLT that re-creates the ADO.NET strongly typed DataSets (download the XSLT files and the code to run XSLT transforms in .NET). You can change the XSLT template (more on templates later) that generates the strongly typed DataSets to alter the code it generates. Your changes might be relatively simple ones, such as modifying the visibility of the columns in each DataTable. You can also make complex changes, such as altering the instantiation of DataTables so you can make effective use of derived classes that contain manual code such as validation. These techniques are also effective for generating code other than DataSets, such as strongly typed arrays or non–data-related code.

You need two things to start creating an XSLT transformation: an XML document for input and a clear idea of what you want the output to look like. The XML Schema Definition (XSD) file that's created when you add a new DataSet to a Visual Studio .NET project is an XML document. You can see it in XML format by selecting the XML tab from the bottom of the DataSet designer. This XML is perfectly valid input, but the XSLT to process it is difficult to read and understand because everything in an XSD is described in terms of types and elements. Your work with XSLT is much easier if you use an XML document that's designed to be friendly to that particular transform.

Figure 1. Transform XML in Steps.

To take this approach, use a preliminary XSLT that transforms the XSD into XML that's then easier to transform into generated code. The second XSLT file (the one that generates code) is far more readable, because you can use familiar terms such as DataSet and DataTable; you hide some of the ugliness of restructuring the data in the preliminary XSLT transformation. This results in a two-step process: Use the XSD to create the XML file containing metadata, then perform the code-generating transformation (see Figure 1).

A quick XSLT primer and a look at a simple transformation will help you understand these steps (see Listing 1). XSLT is a species of XML, and each XSLT instruction is technically an XML element that usually contains attributes necessary for processing. An XSLT stylesheet contains the instructions. Each time a stylesheet is processed, a single output is created. This output is usually a file or display, but you can also use streams in .NET to redirect the output.

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home