Improve XSLT Performance

Get better performance and improved reliability when performing transformations with the Microsoft .NET Framework 2.0 and its XSL classes.

Technology Toolbox: VB.NET, C#, SQL Server 2000, ASP.NET, XML, XSLT, XSL, Visual C# 2005

The Microsoft .NET Framework 2.0 introduces many new tools and improvements for developers, including a rich and powerful set of XML classes that let you tap into XML and Extensible Stylesheet Language Transformations (XSLT) from your applications.

XSLT is a transformation-based formatter based on XML. You use XSLT to transform an XML document into another XML document, or another type of document that a browser can recognize, such as HTML or XHTML. .NET 1.x provided built-in support for transforming XML documents using XSLT, but .NET 2.0 provides huge improvements and performance gains by adding a set of new namespaces and classes to the base class library. I'll show you how to use these new classes to transform an XML document into an HTML document. I'll also explain how to perform operations such as passing parameters to an XSLT stylesheet, invoking extension objects, and utilizing scripts from within an XSL file.

XSLT typically transforms each XML element into an HTML element. XSLT can also add new elements into the output file or remove elements. It can rearrange and sort elements, as well as test and make decisions about which elements to display. You can use XSLT to convert structured XML documents into some other form of text output—HTML, typically, although you can also use it to generate regular text, comma-separated output, more XML, and so on.

XSLT uses the XML Path (XPath) language to perform queries on an XML document to select a particular part of the document. You use an XSLT processor and an XSLT stylesheet (XSLT file) that defines how to carry out the transformation. .NET 2.0 includes a new class named XsltCommand that acts as the new XSLT processor and renders the XslTransform class in .NET 1.x obsolete. Before using the XsltCommand class, you should understand a couple points about the System.Xml.Query namespace. Microsoft introduced this new namespace with .NET 2.0, and it contains two important classes: XsltCommand and XmlArgumentList. XsltCommand is the core class that transforms XML data using an XSLT stylesheet. Microsoft based XsltCommand's implementation on the XML query architecture, and this class provides significant performance gains when compared to the XslTransform class. XsltCommand supports the XSLT 1.0 syntax. You use the XmlArgumentList class to supply runtime parameter values to the XSLT query processor. Runtime parameters can help you utilize the same compiled query multiple times with different parameters. You can also use this class to add a new extension object to the list and associate it with the given namespace. Now that you understand how these two pivotal classes work, it's time to use the XsltCommand class to transform an XML document into HTML.

Implement Simple Transformations
The System.Xml.Query namespace contains the XsltCommand class. You can use this class to transform XML data from any store, as long as the data store implements the IXPathNavigable interface. An XSLT stylesheet that you use in the transformation must include the proper namespace declaration as part of its xsl:stylesheet declaration:


The XsltCommand class includes two key methods: Compile and Execute. You use the Compile method to compile the stylesheet. This method includes several overloads. Traditionally, you would pass in either the XmlReader object or the uniform resource identifier (URI) of the XSLT file as an argument. You use the Execute method to transform the XML data into a specified format using the stylesheet that you loaded previously with the Compile method. It is the Execute method that executes the transform using the compiled XSLT stylesheet.

The example in this article requires that you create a simple Web form named XsltCommandExample.aspx. This Web form displays data from the Products table of the Northwind database. You retrieve the data from the Products table in the form of XML and then apply an external XSLT stylesheet (named ProductsPaging.xsl) on the XML data to emit HTML. Next, write this HTML directly onto the client browser (see Listing 1).

You know how code that carries out the transformation works; next, take a look at an XSLT stylesheet that displays product information in HTML format (see Listing 2 and Figure 1).

Next, you want to build on this example and add the ability to pass parameters to the stylesheet. You do this with the XmlArgumentList class. XmlArgumentList makes it possible for you to create XSLT reusable and maintainable stylesheets by providing a mechanism for passing parameters to the stylesheets. It also gives you the ability to associate a class with the namespace URI, enabling you to call the methods of a class directly from a stylesheet. Objects invoked from the stylesheet in this manner are called extension objects.

The XmlArgumentList class includes a couple important methods that help you achieve this: AddParameter, which lets you add parameters to the XmlArgumentList; and AddExtensionObject, which is similar to AddParameter, but adds a new extension object to the XmlArgumentList object. Once you add the object to the XmlArgumentList object and pass it to the stylesheet, you can then invoke its properties and methods directly from the stylesheet.

The stylesheet contains the required parameters already, so you can simply pass in values to those parameters and override the default parameter values. However, you need to modify the stylesheet for displaying the Next Page and Previous Page links (for paging) to enable the user to navigate to the next or previous set of records (see Listing 3). Next, go back to the XsltCommandExample.aspx file and modify its Page_Load event (see Listing 4).

It's time to check your progress so far. Point your browser to the URL of the Web form and look at the results (see Figure 2).

Invoke Extension Methods
Next, you need to invoke the methods of an extension object from a stylesheet. For instance, assume you want to display each category in the product listings with a different background color. You can accomplish this using extension objects.

Begin by creating a new class named BGColor under the Code directory. This class determines the specific background color to be returned for a given category ID (see Listing 5).

Determining the color is simple enough. Next, you want to invoke its ReturnBGColor method from the stylesheet. Do this by adding the text in bold to the ProductsPaging.xsl file:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl=
<xsl:output method="html" />
<!-- Each record on a separate row -->
<xsl:element name="tr">
   <xsl:attribute name="bgcolor">
      <xsl:value-of select=
      "myColor:ReturnBGColor(CategoryID)" />
   <xsl:element name="td">
      <xsl:value-of select="ProductID" />

You add the attribute xmlns:myColor="urn:myColor" to the xslstylesheet element to associate a namespace URI for your extension object.

You can invoke methods of the object from the stylesheet as if it were part of the stylesheet once you have the association between the object and the namespace URI in place:

<xsl:value-of select=
   "myColor:ReturnBGColor(CategoryID)" />

This line invokes the ReturnBGColor method of the BGColor class, passing to it the category ID. The next step is to pass the extension object to the stylesheet so it can invoke the extension object's methods (see Listing 6).

In the previous example, you used extension objects to execute custom code from the stylesheet. It is also possible for you to create the same functionality by embedding scripts inside the stylesheet (see Listing 7).

The examples described, while interesting, only hint at what's possible using this technology. For example, you might extend this example to retrieve only the required number of rows from the database instead of fetching all the rows from the Products table every time. Apart from performing a simple customization of the user interface, you can also use extension objects to perform sophisticated operations such as table lookup, data validation, backup operations, and so on.

About the Author

Thiru Thangarathinam works at Intel in Chandler, Ariz. He specializes in architecting, designing, and developing .NET-based distributed enterprise-class applications. He has coauthored several books on .NET-related technologies. He has also been a frequent contributor to leading technology-related publications. Reach him at

comments powered by Disqus
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.