XSLT - eXtensible StyleSheet Language Transformations

XSLT stands for eXtensible StyleSheet Language Transformations and In this Article we are going to cover the following concepts about XSLT. 1. What is XSL and how it came into existence? 2. What is XSLT. 3. Advantages of XSLT. 4. Using XSLT in ASP.NET 5. Keywords used in the XSLT file.

XSLT - eXtensible StyleSheet Language Transformations

XSL stands for eXtensible StyleSheet Language. XSL is a StyleSheet Language for XML documents.

As we know that XML does not use predefined tags. We have to define our own tags thus the meaning of each tag is not understood properly.

What is XSL and how it came into existence?

XSL describes how the XML document should be displayed.

XSL comprises of 3 parts as below:

XPATH: It is a Language to navigate in XML documents.
XSL-FO: It is a language for formatting XML documents
XSLT: It is a language for transforming XML document.

In this article we are going to see what is XSLT and how we can use it in ASP.NET.

What is XSLT?
1. XSLT stands for eXtensible StyleSheet Language Transformations.
2. It is a W3C recommendation
3. It can be used to Transform one XML document into another XML document such as XHTML,HTML.
4. It uses XPath in order to navigate in XML documents.

Advantages of XSLT
XSLT transforms each XML element into an (X)HTML element not only this, With XSLT you can add or remove elements and attributes to or from the resultant output file. You can also rearrange elements and sort the elements and we can do lot more things with XSLT.


1. Create a New ASP.NET website. Right click the website in solution explorer and add a new StyleSheet file(XSLT File). This file has the extension .xslt.
2. This adds the XSLT file to your website and opens it in the designer. It contains the following content:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@* | node()">
<xsl:apply-templates select="@* | node()"/>

Let us see what the XSLT file comprises of:
1. The first line is the XMl declaration which indicates that it is an XML file.
2. The Second line declares the document to be an XSL style sheet that is: <xsl:stylesheet>. This element contains version number and XSLT namespace attributes
3. The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace and the version="1.0" attribute. In order to be able to access the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.
4. The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document.

The content inside the <xsl:template> element defines the HTML to be written to the output. The last two lines define the end of the template and the end of the style sheet.

Add an XML file to the website by Right-Clicking the website in solution explorer and Add New Item then select XML file. Rename it as EmpDetails.xml.

Let us transform the following XML document ("EmpDetails.xml") into XHTML:

<?xml version="1.0" encoding="iso-8859-1" ?>
<Name>John Shaw</Name>
<Name>Robert Tiwari</Name>
<Name>Diya Mishra</Name>
<Name>Snehal Shah</Name>

Replace the default XSL Style Sheet ("XSLTFile.xslt") contents with a transformation template as shown below:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<h2>Employee Information</h2>
<table border="1">
<tr bgcolor="#9acd32">
<xsl:for-each select="Employees/Employee">
<xsl:value-of select="Name"/>
<xsl:value-of select="City"/>

An XSLT file consists of one or more set of rules that are called templates.
A template contains rules to be applied when a specified node is matched.

The <xsl:template> element is used to create templates.
The match attribute present in the element is used to associate a template with an XML element. The value of the match attribute is an XPath expression that is match="/" which defines the whole document.

Let us see few elements used in this XSLT document above:

<xsl:value-of>: The <xsl:value-of> element is used to extract the value of a selected node and add it to the output stream of the transformation.

select: The select attribute in the example above selects the Name and City of each XML element. It can also contain an XPath expression like: Employees/Employee/City. The XPath expression works like navigating a file system. For Example: a forward slash (/) selects subdirectories.

<xsl:for-each>: <xsl:for-each> element is used to select every XML element of a specified node-set , loop through the XML elements, and display all of the records.

Now in the code-behind file (Default.cs). We have to write code to display the Transformed XML file (XSLT) in the browser as follows:

First add the following namespace at the top of the Default.cs file:
using System.Xml.XPath;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

The classes in these namespaces are used to display the result in the browser.

Now add the following code in the Page_Load event handler of the page.

protected void Page_Load(object sender, EventArgs e)
XslCompiledTransform myXslTransform;
XPathDocument myXPathDocument;
myXPathDocument = new XPathDocument(Server.MapPath("EmpDetails.xml"));
myXslTransform = new XslCompiledTransform();
//Loads and compiles the style sheet located at the specified URI.
System.IO.StringWriter stWrite = new System.IO.StringWriter();

//Executes the transform using the input document specified by the myXPathDocument object and
//outputs the results to a stringWriter (stWrite). The second argument provides additional run-time arguments.
myXslTransform.Transform(myXPathDocument, null, stWrite);

catch (Exception ex)
Console.WriteLine("Exception: {0}", ex.ToString());

In the above code The XslCompiledTransform class is an XSLT processor It is a new implementation and includes performance gains when compared to the obsolete XslTransform class.
The Load method of the myXslTransform object loads and compiles the style sheet, while the Transform method of the myXslTransform executes the XSLT transform.

Additional Points:

1. Filtering the Output

We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.

<xsl:for-each select="Employees/Employee[City='Hyderabad']">

Above code filters the records to display only those xml elements which have the City as 'Hyderabad'

We can use the following operators to filter the output:

< less than
> greater than
= (equal)
!= (not equal)

We can sort the output, by adding an <xsl:sort> element inside the <xsl:for-each> element in the XSL file as shown below:

<xsl:for-each select="Employees/Employee">
<xsl:sort select="City"/>

The select attribute indicates what XML element to sort on. In the above example it sorts on City


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: