Partager via


addParameter Method

 

Adds parameters into an XSL Transformations (XSLT) style sheet.

JScript Syntax

objXSLProcessor.addParameter(baseName, parameter, namespaceURI);  

Parameters

baseName
The name that will be used inside the style sheet to identify the parameter context.

parameter
In most cases, a number, Boolean, string, IXMLDOMNodeList, or IXMLDOMNode. Passing in a single node will produce a node list that contains one node (shortcut). For MSXML 6.0, 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. However for MSXML3.0, this function cannot remove previously added parameters.

namespaceURI (optional)
An optional namespace.

Example

var xslt = new ActiveXObject("Msxml2.XSLTemplate.6.0");  
var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");  
var xslproc;  
xsldoc.async = false;  
xsldoc.load("sample.xsl");  
if (xsldoc.parseError.errorCode != 0) {  
   var myErr = xsldoc.parseError;  
   WScript.Echo("You have 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("You have error " + myErr.reason);  
   } else {  
      xslproc = xslt.createProcessor();  
      xslproc.input = xmldoc;  
      xslproc.addParameter("param1", "Hello");  
      xslproc.transform();  
      WScript.Echo(xslproc.output);  
   }  
}  

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). For MSXML 6.0, 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. However for MSXML3.0, this function cannot remove previously added parametera.

namespaceURI[in, optional]
An optional namespace.

Return Values

E_FAIL if readyState is READYSTATE_INTERACTIVE.

Example

#include "stdio.h"
#include "msxml6.h"

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(XSLTemplate60));
        
        hr=pStyleSheet.CreateInstance(__uuidof(FreeThreadedDOMDocument60));
        pStyleSheet->async = VARIANT_FALSE;
        hr=pStyleSheet->load("sample.xsl");
        //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(DOMDocument60));
        pInputDoc->async = VARIANT_FALSE;
        hr = pInputDoc->load("books.xml");
        //check on the parser error        
        if(hr!=VARIANT_TRUE)
        {
            return checkParseError(pInputDoc->parseError);
        }        
        
        pInputDoc->async = VARIANT_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);
}

Resource File

The examples in this topic use the following file.

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>

Output

The examples in this topic output the following:

The parameter value was: Hello

Remarks

The addParameter method can be called on transformNode handlers and between transform calls (in asynchronous processing), and further processing will use the updated parameter. Added parameters are referenced by <xsl:param> within the style sheet.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXSLProcessor