ASP.NET XML/XSL Transforms

Transforming an XML document using XSL/T and outputting the results to the browser is a fairly simple task in ASP.NET. The following user control demonstrates the ease with which this can be accomplished. This user control has a parameter for the XML source(xmlSource), and a parameter for the XSL source(xslSource). When placing this user control on a page, simply specify both values (using relative paths, since they are Server.MapPath'ed within the user control) and you're done! The transformed result will be output to Response.Output and sent to the user's browser. You can use this to create a two line ASPX file that simply uses this user control to render its output. By using Output and/or Fragment caching, you can ensure that the CPU load required to transform the XML is minimized.

<%@ Control Language="c#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server" language="c#">
public string xmlSource, xslSource;
void Page_Load(){
    XmlDocument docXml = new XmlDocument();
    XslTransform docXsl = new XslTransform();
//    chapter.Text = docXml.TransformNode(docXsl);

An example of a page using this user control, Output Caching for 1 minute:

<%@ Register TagPrefix="authors" tagname="chapters" src="/controls/chapters.ascx"%>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<authors:chapters id="chapters" runat="server"
    xmlSource="chapter.xml" xslSource="chapter.xsl" />

To test this out, you can use the following two files:

    <!-- Chapter Name -->
    <name>Chapter Name</name>
    <!-- Author -->
        <name>Steven Smith</name>
    <!-- Examples from Chapter -->
            <!-- The number from the book, e.g. 2.3 -->
            <!-- Link to working example -->
            <!-- Link to source code -->
                <link_text>HelloVB.asp source</link_text>
            <!-- Description of the example -->
            <description>Hello World using Classic ASP.</description>


<?xml version="1.0" ?>
<xsl:transform xmlns:xsl="" version="1.0">

<xsl:output method="xml" />

<xsl:template match="/">
   <xsl:apply-templates select="chapter" />

<xsl:template match="/chapter" >
    <b><xsl:value-of select="name"/></b>
   by <xsl:apply-templates select="author" />
   <xsl:apply-templates select="examples" />

<xsl:template match="author" >
    <i><xsl:value-of select="name"/></i>

<xsl:template match="examples" >
    <table bgcolor="#000033">
        <tr bgcolor="#CCCCFF">
        <xsl:apply-templates select="example" />

<xsl:template match="example" >
    <tr bgcolor="#DDDDDD">
        <td><xsl:value-of select="reference"/></td>
                <xsl:attribute name="href">
                    <xsl:value-of select="demo/url"/>
                <xsl:value-of select="demo/link_text"/>
                <xsl:attribute name="href">
                    <xsl:value-of select="source/url"/>
                <xsl:value-of select="source/link_text"/>
        <td><xsl:value-of select="description"/></td>