Поделиться через


IXSLTemplate

 

Used for caching compiled XSL Transformations (XSLT) templates.

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.

  
#include “msxml6.h”  
  
#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_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(   
               _T("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_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(  
                           _T("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_DOMDocument60, 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

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

Requirements

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml6.dll, msxml6.lib (MSXML 6.0)

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

Versioning

Implemented in: MSXML 3.0 and later

See Also

IXSLTemplate Members