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.
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.
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.
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.
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?
[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);
(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);
// Xalan and Xerces termination
2. Above code is a part of a porting from Java identity transformation
such like following:
something.java
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
public void doTransform(Source source, Result result)
throws TransformerException {
TransformerFactory factory = createTransformerFactory();
Transformer transfomer = factory.newTransformer(); // <- Create ID
transformer
transfomer.transform(source, result);
}
I ported this code to following C++ code. Is it correct? or Is there other
better way?
#define IDENTITY_TRANSFORM_XSLT "<?xml version='1.0'?>\
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>\
<xsl:output encoding='UTF-16' />\
<xsl:template match='/'>\
<xsl:copy-of select='*' />\
</xsl:template>\
</xsl:stylesheet>"
int _tmain(int argc, _TCHAR* argv[])
{
XMLPlatformUtils::Initialize();
XalanTransformer::initialize();
{
(something code)
XSLTInputSource inputSource( ** some input source **);
const char* const theStylesheet = IDENTITY_TRANSFORM_XSLT;
istringstream theXSLStream(theStylesheet);
const XSLTInputSource stylesheet(&theXSLStream);
XSLTResultTarget result(resultBuf);
XalanTransformer transformer;
int theResult = -1;
theResult = transformer.transform(parsedSource, stylesheet, result);
(something code)
}
}
I'm sorry a very long question, and sorry I'm not good at English.
Thank you.
Ken
_________________________________________________________________
Windows Vista の店頭販売開始!世界が変わる瞬間を体験しよう
http://www.microsoft.com/japan/ultimatetimes/