Sample ASP Script for XSLT Processor Objects

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Sample ASP Script for XSLT Processor Objects

The following ASP script demonstrates most of the key features of the XSLTProcessor and XSLTemplate objects. It takes the genre parameter from a client-side HTML page and returns a table of all books from the book catalog in the given genre, sorted by title.

The ASP script contains the following functions:

  • The loadSource() function creates a free-threaded DOM document that holds the catalog XML structure. The free-threaded nature of the document is important, because this generates a document that processes on a thread separate from the ASP document itself. This is critical for scalability if either the source or transformation XML is cached.
    Note In MSXML, "free-threaded" means ThreadingModel='Both', and cross-thread marshalling is supported.
  • The getProcessor() function loads an XSLT file and caches it to an XSLTemplate object instantiated in this function. This XSLTemplate object supports the createProcessor method for creating an XSLProcessor object that will be used to activate the XSLT transformation.
  • The transformData() function assigns the input for the processor to the source document, and sets the output to the Response object. The resulting XML structure is converted into a string and sent to the client for this particular case. Finally, the transformData() function calls the transform() method on the XSLProcessor object. This method actually performs the transformation that pumps the XML from the input device through the style sheet, and from there to the output device.
  • The main() function parses the parameters a client sends to the server through the Request object. It tests to see if a genre parameter was passed in either a form or a query string, and assigns a default value of "all" to the parameter if nothing was passed. The processor.addParameter method then passes this value as a parameter to the XSLT style sheet. Note that the style sheet must have this parameter defined beforehand, even if no value is associated with the parameter in the XSLT document.

ASP Script (catalog.asp)

<%@LANGUAGE="JavaScript"%>
<%Response.expires=-1;
// catalog.asp
// Sorts books from a given catalog by title, for a given Genre.
function loadSource(sourceName){
    var xmlDoc=new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
    xmlDoc.async=false;
    xmlDoc.load(Server.mapPath(sourceName));
    return xmlDoc;
    }

function getProcessor(transformName){
    if ("" + Session(transformName) == "undefined"){
        var xslDoc=new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
        var xslTemplate=new ActiveXObject("Msxml2.XSLTemplate.5.0");
        xslDoc.async=false;
        xslDoc.load(Server.mapPath(transformName));
        xslTemplate.stylesheet=xslDoc;
        xslProcessor=xslTemplate.createProcessor();
        Session(transformName)=xslProcessor;
        }
    else {
        xslProcessor=Session(transformName);
        }
    return xslProcessor;
    }

function transformData(srcDoc,processor){
    processor.input=srcDoc;
    processor.output=Response;
    processor.transform();
    Response.Flush();
    return true;
    }

function main(){
    var srcDoc=loadSource("books.xml");
    var processor=getProcessor("catalogFilter.xsl");

    var genre=Request("genre");
    if(""+genre=="undefined"){
        genre="all";
        }
    else {
        genre=""+genre;
        }           
    processor.addParameter("selected_genre",genre);
    transformData(srcDoc,processor);
    }    

main();    
 %>
Note   The following is an alternative implementation of the transformData() function.
function transformData(srcDoc,processor){
    processor.input=srcDoc;
    processor.transform();
    Response.Write(processor.output);
    return true;
}