Microsoft XML SDK 2.6 - XSLT Developer's Guide

Using XSLT in an Active Server Page

XSLT transformations can be performed at the server. Besides the ability to use XSLT in your server-side logic, such as filtering your XML, customizing it, or changing its schema, this enables you to deploy content in XML and transform it to HTML on demand for down-level clients.

The code for performing XSLT transformations in an Active Server Page is similar to that used on the client (see Transforming the Contents of XML Data Islands Using XSLT). The XML source document and the style sheet are loaded, and transformNode is called to invoke the XSLT processor.

<%@ LANGUAGE = JScript %>
<%
  // Set the source and style sheet locations here
  var sourceFile = Server.MapPath("simple.xml");
  var styleFile = Server.MapPath("simple.xsl");
  
  // Load the XML 
  var source = Server.CreateObject("Microsoft.XMLDOM");
  source.async = false;
  source.load(sourceFile);
  // Load the XSLT
  var style = Server.CreateObject("Microsoft.XMLDOM");
  style.async = false;
  style.load(styleFile);
  Response.Write(source.transformNode(style));
%>

The Server.MapPath method resolves a relative URL to a full path. The Server.CreateObject instantiates a new XML DOM Document object. The results of the transformation are sent to the client using the Response.Write method.

The transformNodeToObject method offers the ability to write a transformation directly to the IStream interface of the response object. To use this feature, the last line in the previous example would instead become:

source.transformNodeToObject(style, Response);

This method performs better on the server in most situations, especially for long documents, which would otherwise require a significant memory allocation to hold the complete transformation results.

Try it!   The Breakfast Menu Demo found at XSLT Developer's Guide Samples shows the transformNodeToObject call in action.

See the XSL version of this sample at XSL Developer's Guide Samples.