Microsoft XML SDK 2.6 - XML Reference

IXSLProcessor Interface

Used for transformations with compiled style sheets.

Methods

addObject Allows you to pass objects to a style sheet.
addParameter Allows you to pass variables to a style sheet.
input Specifies which XML input tree to transform.
output Used to provide a custom output object to write the result of a transformation to.
readyState Returns the current state of the processor.
reset Resets the processor state to what it was before calling transform.
setStartMode Allows you to perform subsets of a larger XSLT transformation.
startMode Returns the base name part of the start mode (a qualified name).
startModeURI Returns the namespace URI of the start mode (a qualified name).
stylesheet Returns the style sheet that was defined on the IXSLTemplate at the time this processor object was created.
ownerTemplate Returns the style sheet template used to create the XSL processor object.
transform Starts the transformation process or resumes a transform that previously returned VARIANT_FALSE.

Remarks

The createProcessor method returns an XSLProcessor object. The processor object has a transform method that takes the input data, applies the XSLT style sheet defined in the XSLTemplate, and writes the result to the specified output stream.

This XSL processor is completely independent of the transformNode and transformNodeToObject methods on IXMLDOMDocument. In particular, when you transform XML using this interface, the ontransformnode event is not fired on the DOMDocument object.

The IXSLProcessor interface is designed to handle asynchronous transformations. In this case, multiple calls to transform need to be made, in which each call will transform as much as it can based on what is available from the input. In this scenario, the processor remains in READYSTATE_INTERACTIVE until the transform is complete.

Example

BOOL XSLProcessorDemo()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   HRESULT hr;
   IXMLDOMDocument2 *pStyleSheet=NULL;
   IXMLDOMDocument2 *pDOMObject=NULL;
   IXSLTemplate *pIXSLTemplate=NULL;
   IXSLProcessor *pIXSLProcessor=NULL;
   VARIANT varValue;

   try
   {
      hr = CoCreateInstance(CLSID_XSLTemplate, NULL, CLSCTX_SERVER, 
            IID_IXSLTemplate, (LPVOID*)(&pIXSLTemplate));
      SUCCEEDED(hr) ? 0 : throw hr;
      
      if(pIXSLTemplate)
      {
         hr=CoCreateInstance(CLSID_FreeThreadedDOMDocument, NULL, 
               CLSCTX_SERVER, IID_IXMLDOMDocument2, 
               (LPVOID*)(&pStyleSheet));
         SUCCEEDED(hr) ? 0 : throw hr;
         
         if(pStyleSheet)
         {
            hr=pStyleSheet->put_async(VARIANT_FALSE);
            if(SUCCEEDED(hr))
            {
               hr=pStyleSheet->load(_variant_t 
                  (_T("d:\\inetpub\\wwwroot\\samplexsl.xml")), &sResult);
               if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
               {
                  hr=pIXSLTemplate->putref_stylesheet(pStyleSheet);
                  if(SUCCEEDED(hr))
                  {
                     hr=pIXSLTemplate->createProcessor(&pIXSLProcessor);
                     SUCCEEDED(hr) ? 0 : throw hr;
                     if(pIXSLProcessor)
                     {
                        hr=CoCreateInstance(CLSID_DOMDocument, NULL, 
                           CLSCTX_SERVER, IID_IXMLDOMDocument2, 
                           (LPVOID*)(&pIXMLDOMDocument));
                        SUCCEEDED(hr) ? 0 : throw hr;

                        if(pIXMLDOMDocument)
                        {
                           hr=pIXMLDOMDocument->put_async(VARIANT_FALSE);
                           if(SUCCEEDED(hr))
                           {
                              hr=pIXMLDOMDocument->load(_variant_t( 
                  _T("d:\\inetpub\\wwwroot\\sampleXSLWithObject.xml")),                                  &sResult);
                              if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
                              {
                                 hr=pIXSLProcessor->put_input(_variant_t 
                                    (pIXMLDOMDocument));
                                 if(SUCCEEDED(hr))
                                 {
                                    hr=CoCreateInstance( 
                                       CLSID_FreeThreadedDOMDocument, 
                                       NULL, CLSCTX_SERVER, 
                                       IID_IXMLDOMDocument2, 
                                       (LPVOID*)(&pDOMObject));
                                    if(SUCCEEDED(hr) && pDOMObject)
                                    {
                                       hr=pDOMObject->put_async( 
                                          VARIANT_FALSE);
                                       if(SUCCEEDED(hr))
                                       {
                                          hr=pDOMObject->loadXML( 
                                          _T("<foo>foo</foo>"), &sResult);
                                          if(SUCCEEDED(hr))
                                          {
                                             hr=pIXSLProcessor->addObject(
                                                pDOMObject, _T("urn:my-
                                                object"));
                                             if(SUCCEEDED(hr))
                                                bResult=TRUE;
                                          }
                                       }
                                    }

                                    hr=pIXSLProcessor->transform( 
                                       &sResult);
                                    if(SUCCEEDED(hr)&&(sResult== 
                                       VARIANT_TRUE))
                                    {
                                       pIXSLProcessor->get_output( 
                                          &varValue);
                                       ::MessageBox(NULL, 
                                          _bstr_t(varValue), 
                                          _T("Transformed Output"),
                                          MB_OK);
                                    }
                                    RELEASE(pDOMObject);
                                 }
                              }
                           }
                           RELEASE(pIXMLDOMDocument);
                        }
                     }
                  }
               }
            }
            RELEASE(pStyleSheet);
         }
         RELEASE(pIXSLTemplate);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXSLTemplate);
      CHECK_AND_RELEASE(pStyleSheet);
      CHECK_AND_RELEASE(pIXMLDOMDocument);
      CHECK_AND_RELEASE(pDOMObject);
      DisplayErrorToUser();
   }
   return bResult;
}

The stylesheet – "d:\\inetpub\\wwwroot\\sampleXSLWithObject.xml"

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       version="1.0" xmlns:myObj="urn:my-object">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="bar">
<xsl:value-of select="myObj:get-text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Output (in a message box)

<?xml version="1.0" encoding="UTF-16"?>
<bar>
foo
</bar>

Requirements

Implementation: Msxml2.dll

Header and IDL files: Msxml.h, Xmldom.idl

Version-Independent ProgID: Msxml2.XSLTemplate

Version-Independent CLSID: 2933BF94-7B36-11d2-B20E-00C04F983E60

Version-Dependent ProgID: Msxml2.XSLTemplate.2.6

Version-Dependent CLSID: f5078f21-c551-11d3-89b9-0000f81fe221