Я новичок в области xml и xslt.Я написал простую утилиту COM для преобразования xsl с помощью xslt.Но происходит сбой при вызове функции transformNodeToObject.Я использую Visual Studio 15.
Как вы можете видеть, я использую импорт msxml6.dll.
Был другой тип вызова m_hrDOMInitStatus = CoCreateInstance (MSXML2 :: CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER, MSXML2 :: IID_IXMLDOMDocument3, (void **) & m_pDoc);Не уверен, нужно ли мне использовать этот API
#include "stdafx.h"
#include <windows.h>
#include "iostream"
#include <WTypes.h>
#include <comdef.h>
#include <wchar.h>
#include <vector>
#import <msxml6.dll>
using namespace MSXML2;
using namespace std;
int main()
{
HRESULT hResult = S_OK;
hResult = CoInitialize(NULL);
if (FAILED(hResult))
{
cerr << "Failed to initialize COM environment" << endl;
return 0;
}
// MSXML COM smart pointers
// Use the Document2 class to enable schema validation
IXMLDOMDocument2Ptr spDocSource;
IXMLDOMDocument2Ptr spDocResult;
IXMLDOMDocument2Ptr spDocStylesheet;
struct IDispatch * pDispatch;
// Create the COM DOM Document objects
hResult = spDocSource.CreateInstance(__uuidof(DOMDocument60));
if FAILED(hResult)
{
cerr << "Failed to create Source Document instance" << endl;
return 1;
}
hResult = spDocResult.CreateInstance(__uuidof(DOMDocument60));
if FAILED(hResult)
{
cerr << "Failed to create Result Document instance" << endl;
return 1;
}
hResult = spDocStylesheet.CreateInstance(__uuidof(DOMDocument60));
if FAILED(hResult)
{
cerr << "Failed to create Stylesheet Document instance" << endl;
return 1;
}
// Load the source document
spDocSource->async = VARIANT_FALSE;
hResult = spDocSource->load("xmlinputfile.xml");
if (hResult != VARIANT_TRUE)
{
cout << "Error parsing xmlinputfile.xml" << endl;
return 1;
}
spDocSource->async = VARIANT_FALSE;
hResult = spDocSource->load("XSLTFile1.xslt");
if (hResult != VARIANT_TRUE)
{
cout << "Error parsing XSLTFile1.xml" << endl;
return 1;
}
spDocResult->QueryInterface(IID_IDispatch, (void **)&pDispatch);
VARIANT vResultDoc;
vResultDoc.vt = VT_DISPATCH;
vResultDoc.pdispVal = pDispatch;
hResult = spDocSource->transformNodeToObject(spDocStylesheet, vResultDoc);
if FAILED(hResult)
{
cout << "Error in performing transformation" << endl;
return 1;
}
return 0;
}
xml input:
_____________
<?xml version="1.0"?>
-<MODEL-LIST ENTERPRISE-XML-VERSION="3">
<MODEL TYPE="Enhanced Macrocell" ID="450 MHz Default"> </MODEL>
</MODEL-LIST>
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="xml" indent="yes"/>
<xsl:template match="@* | node()">
<MODEL-LIST>
<xsl:for-each select="MODEL">
<xsl:element name="MODEL">
<xsl:attribute name="ID">
<xsl:value-of select="@ID"/>
</xsl:attribute>
<xsl:attribute name="TYPE">
<xsl:value-of select="@TYPE"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</MODEL-LIST>
</xsl:template>
</xsl:stylesheet>