Microsoft XML 2.5 SDK


 
[This is preliminary documentation and subject to change.]

IXMLDOMXSLTemplate Examples

This topic has several examples that demonstrate how to use the XMLDOMXSLTemplate object.

Typical Caching Scenario

The initial setup of the XSL template goes into an ASP Application state:

<% 
var xsldoc = new ActiveXObject("microsoft.freethreadedxmldom");
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) re-uses the previously loaded template using rental-model objects, as follows:

<%
var xmldoc = new ActiveXObject("microsoft.xmldom");
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 stylesheet while the user ASPs are running, as follows:

<%
var newxsldoc = new ActiveXObject("microsoft.freethreadedxmldom");
newxsldoc.async = false;
newxsldoc.load("SomeNewStyleStyleSheet.xsl");
var myTemplate = Application("template");
myTemplate.stylesheet = newxsldoc;
%>

ADO

The next 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();
%>

Asynchronous Transformations

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("Microsoft.XMLDOM");
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
// 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 your done.
if (xmldoc.readyState == 4)
   XSLOutput.document.close();
}

Passing a Parameter

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 Objects

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...