티스토리 뷰

BOOL CEzXMLParser::ApplyXSLT(TCHAR* xsltPath, CString& szTransformedText)
{
szTransformedText.Empty();

//////////////////////////////////////////////////////////////////////////
// REFERENCE
// http://www.codeguru.com/Cpp/data/data-misc/xml/article.php/c4565
variant_t vResult;
void *output  = NULL;
MSXML2::IXMLDOMDocumentPtr pXslt(__uuidof(FreeThreadedDOMDocument));
IXSLTemplatePtr    pTemplate(__uuidof(XSLTemplate));
IXSLProcessorPtr   pProcessor;
IStream *pOutStream;

try{
// load xml file with data and xsl file to transform
// xml -> html
vResult = pXslt->load( _bstr_t(_T(xsltPath)) );

} catch(_com_error &e) {
TRACE("EzXMLParser : XSLT Load Error");
PrintComError(e);
return FALSE;
}

try{
vResult = pTemplate->putref_stylesheet(pXslt);
pProcessor = pTemplate->createProcessor();
} catch(_com_error &e) {
TRACE("EzXMLParser : Template Creation Error.");
PrintComError( e );
return FALSE;
}

// prepare Stream object to store results of transformation,
// and set processor output to it
CreateStreamOnHGlobal(NULL,TRUE,&pOutStream);
pProcessor->put_output(_variant_t(pOutStream));

// attach to processor XML file we want to transform,
vResult = pProcessor->put_input(_variant_t((IUnknown*)m_pXMLDoc));
pProcessor->transform();

//get results of transformation and print them to stdout
HGLOBAL hg = NULL;
pOutStream->Write((void const*)"\0",1,0);
GetHGlobalFromStream(pOutStream,&hg);
output = GlobalLock(hg);

szTransformedText.Format( "%s", (const char*)output );  // 실제 저장 루틴

GlobalUnlock(hg);

//release before CoUninitialize()
pXslt.Release();
pTemplate.Release();
pProcessor.Release();

return 0;
}

말그대로 XML 문서를 주어진 입력 위치의 XSLT를 이용해서 변환하여 HTML문서로 변환시켜 주는 소스입니다.
문제는 Charset을 잘 맞춰야합니다. xml은 상당히 charset에 민감해서 있어야하 곳에 이것이 빠진다던가 하면 정상적인 동작을 하지 않습니다.