You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Harry Liljeström <ha...@saunalahti.fi> on 2007/01/18 15:16:41 UTC

String variable as stylesheet parameter

Hi,

I have following method:

int Transformer::doTransform(const std::string& profileName)
{
    XALAN_USING_STD(cerr)
    XALAN_USING_STD(endl)
    XALAN_USING_XALAN(XalanTransformer)
    XALAN_USING_XALAN(XalanDOMString)

    int result = -1;

    initialize();

    // Try to open the output file.
    FILE* const outputFile = fopen(fileHandler->GetOutputFilePath().c_str(), "w");

    if( outputFile )
    {
        CallbackHandler cbHandler(outputFile);
        string test("active");

        XalanTransformer xalanTransformer;
        const XalanDOMString keyActive("active");
        const XalanDOMString valActive(profileName.c_str());

        xalanTransformer.setStylesheetParam( keyActive, valActive );

        result = xalanTransformer.transform( fileHandler->GetInputFilePath().c_str(),
                                                               fileHandler->GetTransformationFilePath().c_str(),
                                                               &cbHandler,
                                                               writeCallback,
                                                               flushCallback );

        fclose(outputFile);

        if(result != 0)
        {
            cerr << "XalanError: " << xalanTransformer.getLastError() << endl;
        }
    }
    else
    {
        cerr << "Error: " << "Unable to open output file " << fileHandler->GetOutputFilePath().c_str() << endl;
    }

    finalize();

    return result;
}

The "active" parameter never gets proper value!

I'm wondering how the const XalanDOMString valActive(profileName.c_str()); should be initialized?

If I change the const XalanDOMString valActive(profileName.c_str()); to
const XalanDOMString valActive("'MyTestProfile'");, it works fine. But in my case I'll need a std::string variable. I'm beginner in using Xerces and Xalan so could someone more experienced in this area help me futher?

Thanks,

Harry


Re: String variable as stylesheet parameter

Posted by David Bertoni <db...@apache.org>.
Harry Liljeström wrote:
> Hi,
> 
> I have following method:
> 

...


> 
> The "active" parameter never gets proper value!
>

Well, I guess that depends on what you want the proper value to be.

> I'm wondering how the const XalanDOMString 
> valActive(profileName.c_str()); should be initialized?
> 
> If I change the const XalanDOMString valActive(profileName.c_str()); to
> const XalanDOMString valActive("'MyTestProfile'");, it works fine. But 
> in my case I'll need a std::string variable. I'm beginner in using 
> Xerces and Xalan so could someone more experienced in this area help me 
> futher?

The expression parameter of XalanTransformer::setStylesheetParam() is an 
XPath expression, so the string "active" is interpreted as a location path. 
  If you want a string literal, you need to delimit it with single or 
double quotes:

std::string localProfile("'");
localProfile += profileName;
localProfile += "'";

Note that there is an overload of setStylesheetParam() that accepts const 
char*, so you do not necessarily need to create XalanDOMString instances to 
set the params:

std::string keyActive("active");

xalanTransformer.setStylesheetParam( keyActive.c_str(), localProfile.c_str() );

Dave