Applying XSLT stylesheet to an XML file at runtime

Introduction

 
An XML file can be transformed statically or dynamically. For statically linking an XML file to a stylesheet, the following Processing Instruction can be used:
  1. <?xml-stylesheet type="text/xsl" href="filename.xsl"?> 
     
An XML file can be dynamically linked to a stylesheet by using an instance of the XslCompiledTransform class.
 
XML is a tag-based markup language that is primarily used to transfer data. Being text, XML is supported on a variety of hardware and software platforms. XML supports creating user-defined tags. Since XML is used for data representation, it cannot be used for displaying formatted data. To display formatted data, it uses stylesheet.
 
There are two types of stylesheets, Extensible Stylesheet Language (XSL) and Cascading Stylesheets (CSS). XSL is a superset of CSS. It supports various functions that are not supported by CSS. For example, CSS cannot be used to sort elements or do conditional formatting. Also, XSL is written in a different syntax from CSS. XSL follows the syntax of XML.
 
Following are some of the elements of XSL
  • stylesheet: An XSL file has <stylesheet>as the root element. It is written as follows:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     
  • template: The <template> element is used to specify the formatting rules to be applied when a particular node is matched. It is written as follows:
    <xsl:template match="/">
     
  • for-each: The <for-each> element is used to loop through a particular XML element. It is written as follows:
    <xsl:for-each select="employees/emp">
     
  • value-of: The <value-of> element is used to display the value of a selected node. If the node is an attribute, it is prefixed with @.
    <xsl:value-of select="@id"/>
Following is the sample XML file (emp.xml) that I have used
 
XSLT Stylesheet
 
Following is the sample XSL file (emp.xsl) that I have used
 
Stylesheet
 
Following is the sample XSL file (emptable.xsl) that I have used
 
XSLT Stylesheet with XML
 
An object of the XslCompiledTransformclass can be used to transform the XML file according to the instructions in the XSL file. The XslCompiledTransformclass has a method called Load(), which loads the stylesheet specified as a parameter. The Transformmethod of the XslCompiledTransformclass takes two parameters. The first parameter is the XML file to be transformed and the second parameter is the output HTML file in which the transformed output will be stored.
 
The transformed output can be displayed by navigating to the output file using the Navigate method of the web browser control. A temporary output file is created by generating a temporary file name using the GetTempFileName() method of the Path class. The Path class belongs to the System.IO namespace. The extension of the temporary file is changed from .tmp to .html using the Replace() method of the String class.
 
Following is the code for the transformation
  1. private void btnView_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         // Generating a temporary HTML file  
  6.         string outfile = Path.GetTempFileName().Replace(".tmp"".html");  
  7.         // Creating the XslCompiledTransform object  
  8.         XslCompiledTransform transform = new XslCompiledTransform();     
  9.         // Loading the stylesheet file from the textbox  
  10.         transform.Load(txtXSLFileName.Text);  
  11.         // Transforming the XML file and storing output in HTML file  
  12.         transform.Transform(txtXMLFileName.Text, outfile);  
  13.         // Displaying transformed output on a webbrowser control  
  14.         webBrowser1.Navigate(outfile);  
  15.     }  
  16.     catch (Exception ex)  
  17.     {  
  18.         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  19.     }  
The following code is used to browse for the XML file and display it in a textbox
  1. private void btnBrowseXML_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         FileDialog dialog = new OpenFileDialog();  
  6.         dialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";  
  7.         dialog.FilterIndex = 1;  
  8.         if (dialog.ShowDialog() == DialogResult.OK)  
  9.         {  
  10.             txtXMLFileName.Text = dialog.FileName;  
  11.         }  
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  16.     }  
The following code is used to browse for the XSL file and display it in a textbox:
  1. private void btnBrowseXSL_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         FileDialog dialog = new OpenFileDialog();  
  6.         dialog.Filter = "Stylesheet files (*.xsl)|*.xsl|All files (*.*)|*.*";  
  7.         dialog.FilterIndex = 1;  
  8.         if (dialog.ShowDialog() == DialogResult.OK)  
  9.         {  
  10.             txtXSLFileName.Text = dialog.FileName;  
  11.         }  
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  16.     }  
The following code is used to clear the textboxes and webbrowser control:
  1. private void btnReset_Click(object sender, EventArgs e)  
  2. {  
  3.     txtXMLFileName.Clear();  
  4.     txtXSLFileName.Clear();  
  5.     webBrowser1.Navigate("");  
Note: The following namespaces are required to work with XML related classes:
  1. System.Xml
  2. System.Xml.Xsl
The project is created using Visual C# 2005 Express Edition.
 
Following are the output screens which display the transformed XML file:  

apply XSLT Stylesheet


Similar Articles