This is from the MS XML 4.0 SDK.
You can pass a parameter to an XSL file and have the XSL retrieve the
nodes you
want.
This way you can supply the XML, the XSL and the parameter and it'll
return the
HTML (or output) which you can then response.write to the page.
Example
var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0" );
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
xslDoc.load("sample.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("param1", "Hello");
xslProc.transform();
alert(xslProc.output);
}
File Name: Sample.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="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
[Visual Basic]
Visual Basic Syntax
objXSLProcessor.addParameter(baseName, parameter, namespaceURI)
Parameters
baseName
The name that will be used inside the style sheet to identify the
parameter context.
parameter
A number, Boolean, string, IXMLDOMNodeList, or IXMLDOMNode. Passing in
a single node will produce a node list that contains one node
(shortcut). To remove a parameter previously added to the processor,
provide a value of Empty or Null instead. This acts as a signal to the
processor to remove any previously added parameter of the same name.
namespaceURI (optional)
An optional namespace.
Example
Dim xslt As New Msxml2.XSLTemplate40
Dim xslDoc As New Msxml2.FreeThreadedDOMDocument40
Dim xmlDoc As New Msxml2.DOMDocument40
Dim xslProc As IXSLProcessor
xslDoc.async = False
xslDoc.resolveExternals = False
xslDoc.Load "sample.xsl"
Set xslt.stylesheet = xslDoc
xmlDoc.async = False
xmlDoc.resolveExternals = False
xmlDoc.Load "books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("You have error " & myErr.reason)
Else
Set xslProc = xslt.createProcessor()
xslProc.input = xmlDoc
xslProc.addParameter "param1", "Hello"
xslProc.Transform
MsgBox xslProc.output
End If
File Name: Sample.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="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
[C/C++]
C/C++ Syntax
HRESULT addParameter (BSTR baseName, VARIANT parameter, BSTR
namespaceURI);
Parameters
baseName [in]
The name that will be used inside the style sheet to identify the
parameter context.
parameter [in]
A number, Boolean, string, node list, or node. Passing in a single node
will produce a node list that contains one node (shortcut). To remove a
parameter previously added to the processor, you can pass a value of
VT_EMPTY, VT_NULL, or a NULL IDispatch or IUnknown instead. This acts
as a signal to the processor to remove any previously added parameter
of the same name.
namespaceURI [in, optional]
An optional namespace.
C/C++ Return Values
E_FAIL if readyState is READYSTATE_INTERACTIVE.
Example
#include "stdio.h"
#import <msxml4.dll>
using namespace MSXML2;
int checkParseError(IXMLDOMParseErrorPtr pError);
void dump_com_error(_com_error &e);
int main(int argc, char* argv[])
{
CoInitialize(NULL);
HRESULT hr;
try{
BOOL bResult = FALSE;
short sResult = FALSE;
IXMLDOMDocument2Ptr pStyleSheet=NULL;
IXSLTemplatePtr pIXSLTemplate=NULL;
IXSLProcessorPtr pXSLProcessor=NULL;
hr = pIXSLTemplate.CreateInstance(__uuidof(XSLTemplate4 0));
hr=pStyleSheet.CreateInstance(__uuidof(FreeThreade dDOMDocument40));
pStyleSheet->async = VARIANT_FALSE;
pStyleSheet->resolveExternals = VARIANT_FALSE;
hr=pStyleSheet->load("c:\\samplexsl.xml");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pStyleSheet->parseError);
}
pIXSLTemplate->stylesheet = pStyleSheet.GetInterfacePtr();
pXSLProcessor = pIXSLTemplate->createProcessor();
IXMLDOMDocumentPtr pInputDoc;
hr = pInputDoc.CreateInstance(__uuidof(DOMDocument40));
pInputDoc->async = VARIANT_FALSE;
pInputDoc->resolveExternals = FALSE;
hr = pInputDoc->load("c:\\sampleXSLWithParam.xml");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pInputDoc->parseError);
}
pInputDoc->async = VARIANT_FALSE;
pInputDoc->resolveExternals = FALSE;
pXSLProcessor->input = pInputDoc.GetInterfacePtr();
hr=pXSLProcessor->addParameter("param1", "Hello", "");
VARIANT_BOOL vtRet = pXSLProcessor->transform();
if(vtRet != VARIANT_TRUE)
{
MessageBox(NULL, "transformation failed","Error", MB_OK);
return -1;
}
_bstr_t bstrOutput = pXSLProcessor->Getoutput();
MessageBox(NULL, bstrOutput,"Transformed Output", MB_OK);
}
catch(_com_error &e)
{
dump_com_error(e);
}
return 0;
}
int checkParseError(IXMLDOMParseErrorPtr pError)
{
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline())
+ _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return -1;
}
void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
Style Sheet: "d:\\inetpub\\wwwroot\\sampleXSLWithParam.xml"
<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="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
Output (in a message box)
<?xml version="1.0" encoding="UTF-16"?>
<bar>
Add Parameter Test
</bar>