C ++ XSLT转换无法使用MSXML 3.0

时间:2010-09-03 02:56:45

标签: c++ xml msxml xslt

我是C ++的新手,并继承了以下代码,该代码应该使用XSLT文件转换给定的XML,只是吐出文本值。

它加载了XML和XSLT,并且transformnode()调用返回成功但没有应用转换。底部的原始输出包含原始XML。

它正在使用MSXML 3.0。我已经使用Xselerator来验证XSLT是否有效并且正常工作(即返回字符串“This is a test message ..”)。

这是代码(减去所有错误处理):

IXMLDOMNode *m_pXslt;

ESSXsltData::Initialise(void)
{
    IUnknown *l_pUnknown = NULL;
    IXMLDOMDocument *l_pXSLDocument = NULL;

HRESULT hr = CoCreateInstance(__uuidof(DOMDocument), NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&l_pUnknown);
hr = l_pUnknown->QueryInterface(IID_IXMLDOMDocument,(LPVOID *)&l_pXSLDocument);
CString l_sFileName(RetrieveXsltFileName().c_str() );
hr = l_pXSLDocument->load(l_vFileName, &l_bSuccess);

hr = l_pXSLDocument->QueryInterface(IID_IXMLDOMNode, (LPVOID *)(&m_pXslt) );
}

HRESULT ESSXsltData::ApplyXslt(const char *p_pszESSXml, std::vector< std::string > &p_CommentLines)
{
    IUnknown *l_pUnknown = NULL;
    IXMLDOMDocument *l_pDocument = NULL;


if (p_pszESSXml)
{
    VARIANT_BOOL l_bSuccess;
    HRESULT hr = CoCreateInstance(__uuidof(DOMDocument), NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&l_pUnknown);
    hr = l_pUnknown->QueryInterface(IID_IXMLDOMDocument,(LPVOID *)&l_pDocument);

    hr = l_pDocument->loadXML(CComBSTR(p_pszESSXml) , &l_bSuccess);
    hr = l_pDocument->QueryInterface(IID_IXMLDOMNode, (LPVOID *)(&m_pXslt) );

    BSTR l_bsOutput = NULL;
    hr = l_pDocument->transformNode(m_pXslt, &l_bsOutput);

    COLE2T l_AsciiOutput(l_bsOutput);
    log << "AsciiOutput: " << l_AsciiOutput << "\n"; 
}
}

p_pszESSXml字符串是:

<ESS><Message>This is a test message...</Message></ESS>

XSLT是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="text" indent="yes"/>
    <xsl:template match="ESS">
        <xsl:apply-templates select="Message"/>
    </xsl:template>
    <xsl:template match="Message">
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

修正了它。

重新分配m_pXslt等于XML本应该验证。

有人一直在复制和粘贴。