Understanding 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 XSLT (see Figure 6-5).

click to expand
Figure 6-5: The XSL transformation

The result tree generated after the XML transformation contains element and attribute nodes. 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.

Transformation is required when you need to convert an XML document into an XML schema-matching document. The XslTransform class provides the functionality to implement the XSLT specification. A separate namespace called System.Xml.Xsl defines this class. 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.

Using the Transform Method

The Transform method of XslTransform transforms data using the 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, a stream, TextWriter, or XPathNavigator. (We discuss XPathNavigator in the "Navigating in XML" section of this chapter.)

Follow these steps perform the transformation:

  1. First, you need to create an XslTransform object:

     Dim xslt As XslTransform =  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") 

Looking at an Example

Before you use XslTransform in your application, you need to add three namespace references to your application. These namespaces are System.Xml, System.Xml.XPath, and System.Xml.Xsl. (We discuss the XPath namespace in more detail in the "Navigating in XML" section of this chapter.) Listing 6-19 uses the books.xsl schema file that comes with the .NET Software Development Kit (SDK) samples.

Listing 6-19: An XSL Transformation

start example
 'Create a new XslTransform object and load the stylesheet   Dim xslt As XslTransform = New XslTransform()   xslt.Load("c:\\books.xsl")   'Create a new XPathDocument and load the XML data to be transformed.   Dim mydata As XPathDocument = New XPathDocument("c:\\books.xml")   'Create an XmlTextWriter which outputs 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) 
end example




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net