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.5.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
var xslProc;
xslDoc.async = false;
xslDoc.load("sample2.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
xmlDoc.async = false;
xmlDoc.load("books.xml");
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.transform();
alert(xslProc.output);
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>
Dim xslt As New Msxml2.XSLTemplate50 Dim xslDoc As New Msxml2.FreeThreadedDOMDocument50 Dim xmlDoc As New Msxml2.DOMDocument50 Dim xslProc As IXSLProcessor xslDoc.async = False xslDoc.Load "sample2.xsl" Set xslt.stylesheet = xslDoc xmlDoc.async = False xmlDoc.Load "books.xml" Set xslProc = xslt.createProcessor() xslProc.input = xmlDoc xslProc.Transform MsgBox xslProc.output
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>
#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 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 style sheet "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>
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.
MSXML 2.6 and later
Implementation: msxml5.dll, msxml2.lib
Header and IDL files: msxml2.h, msxml2.idl
Version-Dependent ProgID: Msxml2.XSLTemplate.
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
in the upper-left corner of the page.