IXSLProcessor

Used for transformations with compiled style sheets.

IXSLProcessor is designed to handle asynchronous transformations. In this case, multiple calls to the transform method must 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.

var xslt = new ActiveXObject("Msxml2.XSLTemplate.6.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
var xslProc;
xslDoc.async = false;
xslDoc.load("sample2.xsl");
if (xslDoc.parseError.errorCode != 0) {
   var myErr = xslDoc.parseError;
   WScript.Echo("Stylesheet error: " + myErr.reason);
} else {
   xslt.stylesheet = xslDoc;
   var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
   xmlDoc.async = false;
   xmlDoc.load("books.xml");
   if (xmlDoc.parseError.errorCode != 0) {
      var myErr = xmlDoc.parseError;
      WScript.Echo("Document error: " + myErr.reason);
   } else {
      xslProc = xslt.createProcessor();
      xslProc.input = xmlDoc;
      xslProc.transform();
      WScript.Echo(xslProc.output);
   }
}

The JScript examples uses the following file.

Sample2.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html"/>
   <xsl:param name="param1"/>
   <xsl:template match="/">
       Hello
   </xsl:template>
   <xsl:template match="/" mode="edit">
       In Edit Mode
   </xsl:template>
   <xsl:template match="/" mode="view">
       In View Mode
   </xsl:template>
</xsl:stylesheet>


#include "msxml6.h"

#define CHECK_AND_RELEASE(pInterface)  \
if(pInterface) \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

#define RELEASE(pInterface)  \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\


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

   try
   {
  hr = CoInitialize(NULL);
      SUCCEEDED(hr) ? 0 : throw hr;

     hr = CoCreateInstance(CLSID_XSLTemplate60, NULL, CLSCTX_SERVER, 
            IID_IXSLTemplate, (LPVOID*)(&pIXSLTemplate));
      SUCCEEDED(hr) ? 0 : throw hr;
      
      if(pIXSLTemplate)
      {
         hr=CoCreateInstance(CLSID_FreeThreadedDOMDocument60, 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(L"Sample2.xsl"), &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_DOMDocument60, 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(L" sampleXSLWithObject.xml") , &sResult);
                              if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
                              {
                                 hr=pIXSLProcessor->put_input(_variant_t 
                                    (pIXMLDOMDocument));
                                 if(SUCCEEDED(hr))
                                 {
                                    hr=CoCreateInstance( 
                                       CLSID_FreeThreadedDOMDocument60, 
                                       NULL, CLSCTX_SERVER, 
                                       IID_IXMLDOMDocument2, 
                                       (LPVOID*)(&pDOMObject));
                                    if(SUCCEEDED(hr) && pDOMObject)
                                    {
                                       hr=pDOMObject->put_async( 
                                          VARIANT_FALSE);
                                       if(SUCCEEDED(hr))
                                       {
                                          hr=pDOMObject->loadXML( 
                                          _T("<fff>fff</fff>"), &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;
}

Resource File

The C++ example uses the following file.

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="bbb">
<xsl:value-of select="myObj:get-text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>


Output

The C++ example outputs the following in a message box.

<?xml version="1.0" encoding="UTF-16"?>
<bbb>
fff
</bbb>

The createProcessor method returns IXSLProcessor. The processor has a transform method that takes the input data, applies the XSL Transformations (XSLT) style sheet defined in the IXSLTemplate, and writes the result to the specified output stream.

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

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml4.dll, msxml2.lib (MSXML 4.0)

msxml5.dll, msxml2.lib (MSXML 5.0 for Microsoft Office Applications)

Header and IDL files: msxml2.h, msxml2.idl

Implemented in: MSXML 2.6 and later

Show:
© 2014 Microsoft