XSL transformations can be performed at the server. Besides the ability to use XSL 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 XSL transformations in an Active Server Page is similar to that used on the client (see Transforming the Contents of XML Data Islands Using XSL). The XML source document and the XSL style sheet are loaded, and transformNode is called to invoke the XSL 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 XSL 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 above 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 (Active Server Pages) shows the transformNodeToObject call in action.