Partager via


Using XSLT within ASP

 

You can perform XSLT on the server to filter or customize XML, or to change the XML Schema. You can also deploy content in XML, and transform it to HTML on demand for clients.

The code for performing XSLT within an Active Server Pages (ASP) file is similar to that used on the client. For more information, see Transforming XML Data Islands in an HTML Page.

First, load the XML source document and XSLT file. Then, call transformNode to link to 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("Msxml2.DOMDocument.6.0");
  source.async = false;
  source.load(sourceFile);
  // Load the XSLT.
  var style = Server.CreateObject("Msxml2.DOMDocument.6.0");
  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 method instantiates a new XML DOM Document object. The results of the transformation are sent to the client by the Response.Write method.

The transformNodeToObject method allows you to write a transformation directly to the IStream interface of the Response object. To use this feature, change the last line in the preceding example to the following:

source.transformNodeToObject(style, Response);

In most situations, this method performs better on the server than the transformNode method. This is especially true for long documents, which require a significant memory allocation to hold the complete transformation results.

For an example of a transformNodeToObject call, see the simple.asp file in XSLT Sample: Breakfast Menu.