SIGN UP MEMBER LOGIN:    
ARTICLE

Transformation and XSLT

Posted by Mahesh Chand Articles | XML in C# November 27, 2009
In this article I will explain you about Transformation and XSLT.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

IT'S GOOD THINK YOU

Posted by mohamed youssouf Aug 13, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor