Transformation and XSLT

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

Extensible Stylesheet Language (XSL) is a language for expressing stylesheets. Stylesheets format XML documents in a way so that the XML data can be presented in a certain structure in a browser or other media such as catalogs books and so on.

The XML stylesheet processor reads an XML document (Called an XML source tree) and stylesheet, and it presents the document data in an XML tree format. This processing is XSL Transformation (XSLT). See figure 6-9.

figure-6-9.gif

Figure 6-9. XSL transformation

The result tree generated after XML transformation contains element and attribute nodes. The result tree is also called an element attribute or tree. In this tree, an object is an XML element, and properties are attribute- value pairs.

The XSL stylesheet plays a vital role in the XSLT process. A stylesheet contains a set of tree construction rules, which have two parts. The first part is a pattern of elements in the source tree, and the second is a template for the result tree. The XSL parser reads the pattern and elements from the source tree and then generates results according to the result tree template.

XSLT in .NET

In the .NET Framework, the XslTransform class implements the XSLT specification. This class you defined in a separate namespace called System.Xml.Xsl. Make sure you add a reference to this namespace before using the XslTransform class. You can use the XsltException class to handle exceptions thrown by an XSLT transformation.

The Transform Method

The Transform Method of XslTransaforms data using loaded stylesheet and outputs the result depending on the argument. This method has eight overloaded forms. You can write output of Transform in the form of XmlWriter, stream, TextWriter, or XPathNavigator. (I'll discuss XPathNavigator later in this article.)

Transforming a Document

Follow these steps to perform the transformation:

1. First you need to create an xslTransform object:


        XslTransform xslt = new XslTransform();


2. Now, you load the stylesheet using the Load method:


        xslt.Load("stylesheetFrmt.xsl");


3. Finally, call the Transform method of XslTransform:


        xslt.Transform("xmlfile.xml", "file.html");


Example

Before you use XslTransform in your application, you need to add couple of namespace references to your application. These namespace are Sysem.Xml, System.Xml.XPath, and System.Xml.Xsl. (I'll discuss the X path namespace in more detail in the "Navigation in HTML" section of this article.) This example uses the books.xsl schema file that comes with the .NET SDK sample (see listing 6-25).

Listing 6-25. XSL Transformation sample code

       
// Create a new XslTransform object and load the stylesheet
        XslTransform xslt = new XslTransform();
        xslt.Load(@"c:\books.Xsl");

       
// Create a new XPathDocument and load the XML data to be transformed.
        XPathDocument mydata = new XPathDocument(@"c:\books .xml");

       
// Create an XmlTextWriter which output to the console.
        XmlWriter writer = new XmlTextWriter(Console.Out);

       
// Transform the data and send the output to the console.
        xslt.Transform(mydata, null, writer);


Conclusion

Hope this article would have helped you in Understanding Transformation and XSLT. See other articles on the website also for further reference.

adobook.jpg

This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.