Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Reference

IXSLTemplate

Used for caching compiled XSL Transformations (XSLT) templates.

[C/C++]

Example

The following C++ example demonstrates the use of a single style sheet with multiple XML files. The style sheet is loaded once and cached into the IXSLTemplate object.

#import "msxml5.dll"
using namespace MSXML2;

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

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


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

   try
   {
      hr = CoCreateInstance(CLSID_XSLTemplate50, NULL, CLSCTX_SERVER, 
         IID_IXSLTemplate, (LPVOID*)(&pIXSLTemplate));
      SUCCEEDED(hr) ? 0 : throw hr;
      
      if(pIXSLTemplate)
      {
         hr=CoCreateInstance(CLSID_FreeThreadedDOMDocument50, 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_DOMDocument50, 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\\samplexmldtd.xml")),
                                 &sResult);
                              if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
                              {
                                 hr=pIXSLProcessor->put_input(_variant_t(
                                    pIXMLDOMDocument));
                                 if(SUCCEEDED(hr))
                                 {
                                    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(pIXMLDOMDocument);
                        }
                           // Load another document.
                        hr=CoCreateInstance(CLSID_DOMDocument50, NULL,
                              CLSCTX_SERVER, IID_IXMLDOMDocument2, 
                                 (LPVOID*)(&pIXMLDOMDocument2));
                        SUCCEEDED(hr) ? 0 : throw hr;

                        if(pIXMLDOMDocument2)
                        {
                           hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE);
                           if(SUCCEEDED(hr))
                           {
                              // Load some XML into the document.
                              hr=pIXMLDOMDocument2->loadXML(
                                 _T("<COLLECTION\ xmlns:dt=\"urn:schemas-
                                 microsoft-\ com:datatypes\">\
                                 <DATE dt:dt=\"datetime\">1998-10-
                                       13T15:56:00\ 
                                          </DATE><BOOK><TITLE>Lover Birds</TITLE> \
                                 <AUTHOR>Cynthia Randall</AUTHOR> \
                                 <PUBLISHER>Lucerne Publishing</PUBLISHER> \
                                 </BOOK> \
                                 </COLLECTION>"), &sResult);
                              if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
                              {
                                 // Use the same processor.
                                 hr=pIXSLProcessor->put_input(_variant_t(
                                    pIXMLDOMDocument2));
                                 if(SUCCEEDED(hr))
                                 {
                                    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(pIXMLDOMDocument2);
                        }
                        RELEASE(pIXSLProcessor);
                     }
                  }
               }
            }
            RELEASE(pStyleSheet);
         }
         RELEASE(pIXSLTemplate);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXSLProcessor);
      CHECK_AND_RELEASE(pIXSLTemplate);
      CHECK_AND_RELEASE(pStyleSheet);
      CHECK_AND_RELEASE(pIXMLDOMDocument);
      CHECK_AND_RELEASE(pIXMLDOMDocument2);
      DisplayErrorToUser();
   }
   return bResult;
}

Remarks

Before MSXML 2.6, every call to the transformNode or transformNodeToObject method had to recompile the style sheet because the style sheet was simply passed as an IXMLDOMNode. You can increase performance by caching the compiled style sheet and reusing it with the IXSLTemplate object.

To cache a compiled XSLT style sheet, load an XSLT style sheet into an IXSLTemplate object. This object is free-threaded and stateless, so it can be stored in shared Active Server Pages (ASP) application state. Then to transform a given document using this template, create an IXSLProcessor object using the createProcessor method. The IXSLProcessor object stores the state for one transform call and has a rental-threading model.

Note In MSXML, "free-threaded" means ThreadingModel='Both', and cross-thread marshalling is supported.

This object is an extension of the World Wide Web Consortium (W3C) Document Object Model (DOM).

Versioning

MSXML 2.6 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

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

Version-Dependent ProgID: Msxml2.XSLTemplate.5.0

Version-Dependent CLSID: 88D969C3-F192-11D4-A65F-0040963251E5

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IXSLTemplate Members