Karaki Kenichi wrote: > Hi, everyone! > > I'm a new Xalan-C user and not familiar with C++. > > I want to get wchar_t* from XSLTResultTarget of > XalanTransformer.transform() in order to pass the pointer to other > vendor library for creating the vendor original > string object. So I have to write the XalanTransformer output to memory > buffer.
There is no standard way to get result in a wchar_t because wchar_t is not very portable. On some platforms, wchar_t is encoded in UTF-16, but on others, it's encoded in UTF-32, or even something else. > The string creating function of that library accept wchar_t* to create > Unicode > (UTF-16) string. > And I described "xsl:output encoding='UTF-16'" at my stylesheet. > > I tried the following code, but it didn't work well. > My platform is IA32, OS is Windows XP Pro and IDE is Visual Studio 2003. If your only platform is Windows, then you can guarantee that wchar_t will always be encoded in UTF-16, so you can simply > > Please tell me some advices for my questions. > > > 1. What kind of stream should I use to get output and pass the pointer > easily? > > A. Tried using ostringstream > Transform done fine, but I can't get wcha_t*. > I observed the memory, and write fine. But I can't get string using > str() > because the memory buffer include null byte(0x00). And I don't know > how to get the memory buffer pointer directly, maybe it's a problem > of my C++ skills... > But if this is better way, please tell me how to get the buffer pointer. You shouldn't use std::ostringstream because the performance will be terrible. > > B. Tried using wostirngstream > transform() returns 0(success), but the memory buffer was > 0xff 0xfe 0x00 0x00 0x3c 0x00 0x3f 0x00 ... > I don't know why null code inserted after BOM. > You shouldn't use std::wostringstream either, because the performance will be terrible, and unless you're doing the right thing, you'll get broken results. The result of a transformation is a stream of bytes, regardless of what the encoding is, and wostringstream implements a string of wide characters. > C. Tried using XalanDOMString and PrintWriter > because XSLTResultTarget accept Writer class object. > But, when I executed transform(), I encounterd the assertion > "npos == 0 | ..." > What's wrong? Not enough initializing? > Well, you provided an incomplete assertion, and didn't bother to include the file and line information, so it's impossible to say what went wrong. There are no "levels" of initialization, so, if you've initialized both Xerces-C and Xalan-C properly, it's not an issue of initialization. > > [Snippet code] // Xerces and Xalan initialization > > // XSLTInputSource source > // XSLTInputSource stylesheet (xsl:output encoding='UTF-16') > > (A. Tried using ostringstream) > ostringstream resultBuf; > XSLTResultTarget result(resultBuf); > > (B. Tried using wostringstream) > wostringstream resultBufW; > XSLTResultTarget result((XSLTResultTarget::StreamType&)resultBufW); Any time you are forced to use a C-style cast to get code to compile, you can be assured you're doing something wrong. XSLTResultTarget::StreamType is a typedef for std::ostream, and std::wostringstream is not derived from std::ostream. > > (C. Tried using XalanDOMString and PrintWriter) > XalanDOMString domStr; > DOMStringPrintWriter domSPW(domStr); > XSLTResultTarget result(&domSPW); > > XalanTransformer transformer; > int theResult = -1; > theResult = transformer.transform(source, stylesheet, result); This one should work, and that fact that you're getting an assert means there's probably a bug in Xalan-C, but it's hard to say without seeing the rest of the assert. This might not be the most efficient way of going this, depending on what you'll do with the result. For example, you could write a variation of DOMStringPrintWriter that appends the "vendor string object" directly, but I don't know what those APIs look like, so it's hard to say what might be better. Dave