This topic has several examples that demonstrate how to use the XSLTemplate object.
The initial setup of the XSL template goes into an ASP Application state:
<% var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); xsldoc.async = false; xsldoc.load("someStylesheet.xsl"); var myTemplate.stylesheet = xsldoc; Application("template") = myTemplate; %>
Each client user ASP (which could be hundreds of simultaneous users) reuses the previously loaded template using rental-model objects, as follows:
<% var xmldoc = new ActiveXObject("Msxml2.DOMDocument"); xmldoc.async = false; xmldoc.load("someData.xml"); var myProc = Application("template").createProcessor(); myProc.input = xmldoc; myProc.output = Response; //write response to client myProc.transform(); %>
Then, a client administrator ASP can also safely do a "live" update of the style sheet while the user ASPs are running, as follows:
<% var newxsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); newxsldoc.async = false; newxsldoc.load("SomeNewStyleStyleSheet.xsl"); var myTemplate = Application("template"); myTemplate.stylesheet = newxsldoc; %>
This example connects an ADO Recordset with a cached XSL template and sends the result directly down the wire:
<% var xslprocessor = Application("xsltemplate").createProcessor(); xslprocessor.input = recordset; //take input from ADO recordset xslprocessor.output = Response; //send result to the client xslprocessor.transform(); %>
The following example shows how to accomplish an asynchronous transformation:
var xmldoc; //XMLDOM document for downloading data var XSLProcessor; //XSL transformation engine var XSLOutput; //HTML window to write results to. This function // will kick off the data download and wire up the async callbacks // that Msxml makes during the download. XSLTemplate is the compiled // stylesheet. function NewXMLWindow(dataURL, XSLTemplate) { xmldoc = new ActiveXObject("Msxml2.DOMDocument"); xmldoc.ondataavailable = TransfromChunk; xmldoc.onreadystatechange = TransformChunk; XSLProcessor = XSLTemplate.createProcessor(); XSLProcessor.input = xmldoc; xmldoc.async = true; xmldoc.load(dataUrl); XSLOutput = window.open("XSLOutput","","resizable,scrollbars"); XSLOutput.title = "XSL Output"; XSLOutput.document.open(); return XSLOutput; } // This is the async callback. Now you can transform the next chunk // (as much as is possible, based on how much data was downloaded). // You can then get the chunk of transformed output and write it to // an HTML frame that incrementally displays the results. function TransformChunk() { XSLProcessor.transform(); var chunk = XSLProcessor.output; XSLOutput.document.write(chunk); // If you're done downloading the data, close // the HTML document so that it knows you are done. if (xmldoc.readyState == 4) XSLOutput.document.close(); }
Using a variable in XSL:
<xsl:stylesheet xmlns:xsl="..."> <xsl:param name="myBaseName" /> ... <xsl:value-of select="$myBaseName" /> ... </xsl:stylesheet>
Using a variable in JScript:
var myVariable = 5; //...load XML, XSL, and create xslTemplate here var myProc = myTemplate.createProcessor(): myProc.input = xmldoc; myProc.addParameter(myVariable, "myBaseName"); //now you can perform the transformation.
Passing an object in XSL:
<xsl:stylesheet xmlns:xsl="..." xmlns:myObjNSPrefix="myObjURI"> ... <xsl:value-of select="myObjNSPrefix:someMethod()" /> <xsl:value-of select="myObjNSPrefix:get-someProperty()" /> ... <msxsl:script implements-prefix="foo"> myObjNSPrefix.someMethod(); myObjNSPrefix.someProperty = "bar"; <msxsl:script> </xsl:stylesheet>
Passing an object in script:
function someMethod() { ... } var myObj = new Object; myObj.someMethod = someMethod; myObj.someProperty = 123; //...load XML, XSL, and create xslTemplate var myProc = myTemplate.createProcessor(); myProc.input = xmldoc; myProc.addObject(myObj, "myObjURI"); //...now do the transformation...