Transformation and XSLT in VB.NET
In this article I will explain you about Transformation and XSLT.
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. 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:
Dim xslt As New XslTransform()
2. Now, you load the stylesheet using the Load method:
3. Finally, call the Transform method of XslTransform:
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
Dim xslt As New XslTransform()
' Create a new XPathDocument and load the XML data to be transformed.
Dim mydata As New XPathDocument("c:\books .xml")
' Create an XmlTextWriter which output to the console.
Dim writer As XmlWriter = New XmlTextWriter(Console.Out)
' Transform the data and send the output to the console.
xslt.Transform(mydata, Nothing, writer)
Hope this article would have helped you in Understanding Transformation and XSLT. See other articles on the website also for further reference.