XMLDOMXSLTemplate and XMLDOMXSLProcessor Objects

Beginning with the third release of the Microsoft XML parser, the XMLDOMXSLTemplate object can be used to hold an instance of an XSL or XSLT template. You can create an XMLDOMXSLTemplate object variable in an ASP application for XSL or XSLT documents that may be used frequently or by multiple ASP documents. Since the XSL or XSLT documents will be stored in the XMLDOMXSLTemplate object variable, they will not need to be reloaded or compiled when they are needed. The XMLDOMXSLProcessor object is designed to work with the XMLDOMXSLTemplate object to transform a given document. The XMLDOMXSLTemplate object implements the IXMLDOMXSLTemplate interface and the XMLDOMXSLProcessor object implements the IXMLDOMXSLProcessor interface. The methods and properties of the IXMLDOMXSLTemplate and IXMLDOMXSLProcessor interfaces are as follows:

IXMLDOMXSLTemplate Property

Name Description
Stylesheet Allows an XSL style sheet to compile to an XSL template

IXMLDOMXSLTemplate Method

Name Description
CreateProcessor Creates an XMLDOMXSLProcessor object that can use the template

IXMLDOMXSLProcessor Properties

Name Description
input Sets the XML document that will be transformed.
output Retrieves the output object that will hold the results of the transformation. Can be any object that supports IStream interface, IPersistStream interface, IXMLDOMDocument interface, ASP Response object, and ADODB.stream object.
readyState The current state of the processor object.
startMode The base name of the start mode.
startModeURI Returns the namespace URI of the start mode.
stylesheet The XSL style sheet that will be used to perform the transformation.
ownerTemplate Returns the style sheet template that was used when creating the XSL processor object.

IXMLDOMXSLProcessor Methods

Name Description
addObject (object, namespaceURI) Used to pass objects (such as an XMLDOMDocument object) to a style sheet.
AddParameter (baseName, parameter, namespaceURI) Used to pass variables into a style sheet. The variables will be referenced by the base name. The parameter can be a number, Boolean, string, XMLDOMNodeList object or XMLDOMNode object. The namespace is optional. You can reference these variables in the style sheet by using <xsl:param>.
Reset() Resets the processor object back to the state the object was in prior to calling the transform method.
SetStartMode (mode, namespace)

This method allows you to perform subsets of a larger XSL transformation. Using this method is the same as adding the following to the XSL style sheet:

  <xsl:template match="/">
     <xsl:apply-templates select="*"
The namespace is optional.
Transform() Used to begin the transformation or resume a transformation that returned VARIANT_FALSE.

To see how to use these objects in ASP pages, create a new ASP application. In the global ASP page add the following code:

  Sub Application_OnStart
  Dim l_oXSLDoc
  Dim g_oAcmePOXSLTemplate
  Set l_oXSLDoc = CreateObject("Msxml2.FreeThreadedDOMDocument")
  Set g_oNorthwindPOXSLTemplate=CreateObject("Msxml2.XSLTemplate")
  l_oXSLDoc.async = false
  l_oXSLDoc.load("C:\chapter 12\NorthwindPODOM.xsl")
  Set g_oNorthwindPOXSLTemplate.stylesheet = l_oXSLDoc
  Set Application("template") = g_oNorthwindPOXSLTemplate
  End Sub

In this example, we created a local document object variable and an XMLDOMXSLTemplate object variable that will have application wide scope, and then we loaded the NorthwindPODOM.xsl XSL document. This document will be compiled and stored in the g_oNorthwindPOXSLTemplate variable and can be used by multiple ASP documents and clients at the same time. To reference this document in an ASP document named NPODOM.asp, we would need to add the following code to the ASP document:

  <%@ Language="VBScript" %>
  Dim l_oXMLDoc
  Dim l_oXMLProccessor
  Set l_oXMLDoc = CreateObject("Msxml2.DOMDocument")
  l_oXMLDoc.async = false
  l_oXMLDoc.load("C:\chapter 12\NorthwindPO.xml")
  set l_oXMLProccessor = Application("template").createProcessor()
  l_oXMLProccessor.output = Response
  l_oXMLProccessor.input = l_oXMLDoc

In this case, the NorthwindPO.xml document has been transformed in the ASP page using the XSL document stored in the application variable named template.