I'm stumped big time, I am trying to validate a loded DOMDocument against a schema, this is the code i use:
_bstr_t Validate(LPSTR sXML, LPSTR sXSD)
{
_bstr_t sReturn = (_bstr_t)"";
BSTR bstrSchemaName = (BSTR)"x-schema:XQL";
MSXML2::IXMLDOMDocument2 *pXSDDoc;
MSXML2::IXMLDOMDocument2 *pTestDoc;
MSXML2::IXMLDOMSchemaCollection *pXMLSchema;
MSXML2::IXMLDOMParseError *pError;
HRESULT hr;
try
{
hr = CoInitialize(NULL);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pTestDoc->put_async(VARIANT_FALSE);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_SERVER,
IID_IXMLDOMSchemaCollection, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
/*VARIANT varValue;
varValue.vt = VT_DISPATCH;
varValue.pdispVal = pXMLSchema;
hr = pTestDoc->putref_schemas(varValue);
SUCCEEDED(hr) ? 0 : throw hr;
//if this succeeds loading then the XML is Valid
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is Valid.";
}
else
{
sReturn = (_bstr_t)"Error, XML is not Valid.";
}*/
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
BSTR sReason = (BSTR)"";
hr = pXSDDoc->get_parseError(&pError);
if(SUCCEEDED(hr) && (pError))
{
pError->get_reason(&sReason);
sReturn = (_bstr_t)sReason;
}
else
sReturn = (_bstr_t)"in catch";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;
}
The problem I'm having is the "Add" function, whenever that line of code is reached it jumps to the "catch" block and the error is "Invalid at the top level of the document."
Does anyone have any idea whats cuasing this error (XML and XSD strings are below)?
Thank you.
XML: <?xml version=''1.0''?><COLLECTION xmlns="x-schema:XQL" xmlns:dt="urn:schemas-microsoft-com:datatypes"><DATE dt:dt="datetime">1998-10-13T15:56:00</DATE><BOOK><TITLE>Cosmos</TITLE><AUTHOR>Carl Sagan</AUTHOR><PUBLISHER>Ballantine Books</PUBLISHER></BOOK><BOOK><TITLE>Catwings</TITLE><AUTHOR>Ursula K. Le Guin</AUTHOR><PUBLISHER>Scholastic</PUBLISHER></BOOK><BOOK><TITLE>Home Town</TITLE><AUTHOR>Tracy Kidder</AUTHOR><PUBLISHER>Random House</PUBLISHER></BOOK></COLLECTION>
XSD: <?xml version="1.0"?><Schema xmlns="urn:schemas-microsoft-com:xml-data"><ElementType name="TITLE" /><ElementType name="AUTHOR" /><ElementType name="PUBLISHER" /><ElementType name="DATE" /><ElementType name="BOOK" model="closed"><element type="TITLE" /><element type="AUTHOR" /><element type="PUBLISHER" /></ElementType><ElementType name="COLLECTION" model="closed"><element type="BOOK" /></ElementType></Schema>
_bstr_t Validate(LPSTR sXML, LPSTR sXSD)
{
_bstr_t sReturn = (_bstr_t)"";
BSTR bstrSchemaName = (BSTR)"x-schema:XQL";
MSXML2::IXMLDOMDocument2 *pXSDDoc;
MSXML2::IXMLDOMDocument2 *pTestDoc;
MSXML2::IXMLDOMSchemaCollection *pXMLSchema;
MSXML2::IXMLDOMParseError *pError;
HRESULT hr;
try
{
hr = CoInitialize(NULL);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pTestDoc->put_async(VARIANT_FALSE);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_SERVER,
IID_IXMLDOMSchemaCollection, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
/*VARIANT varValue;
varValue.vt = VT_DISPATCH;
varValue.pdispVal = pXMLSchema;
hr = pTestDoc->putref_schemas(varValue);
SUCCEEDED(hr) ? 0 : throw hr;
//if this succeeds loading then the XML is Valid
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is Valid.";
}
else
{
sReturn = (_bstr_t)"Error, XML is not Valid.";
}*/
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
BSTR sReason = (BSTR)"";
hr = pXSDDoc->get_parseError(&pError);
if(SUCCEEDED(hr) && (pError))
{
pError->get_reason(&sReason);
sReturn = (_bstr_t)sReason;
}
else
sReturn = (_bstr_t)"in catch";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;
}
The problem I'm having is the "Add" function, whenever that line of code is reached it jumps to the "catch" block and the error is "Invalid at the top level of the document."
Does anyone have any idea whats cuasing this error (XML and XSD strings are below)?
Thank you.
XML: <?xml version=''1.0''?><COLLECTION xmlns="x-schema:XQL" xmlns:dt="urn:schemas-microsoft-com:datatypes"><DATE dt:dt="datetime">1998-10-13T15:56:00</DATE><BOOK><TITLE>Cosmos</TITLE><AUTHOR>Carl Sagan</AUTHOR><PUBLISHER>Ballantine Books</PUBLISHER></BOOK><BOOK><TITLE>Catwings</TITLE><AUTHOR>Ursula K. Le Guin</AUTHOR><PUBLISHER>Scholastic</PUBLISHER></BOOK><BOOK><TITLE>Home Town</TITLE><AUTHOR>Tracy Kidder</AUTHOR><PUBLISHER>Random House</PUBLISHER></BOOK></COLLECTION>
XSD: <?xml version="1.0"?><Schema xmlns="urn:schemas-microsoft-com:xml-data"><ElementType name="TITLE" /><ElementType name="AUTHOR" /><ElementType name="PUBLISHER" /><ElementType name="DATE" /><ElementType name="BOOK" model="closed"><element type="TITLE" /><element type="AUTHOR" /><element type="PUBLISHER" /></ElementType><ElementType name="COLLECTION" model="closed"><element type="BOOK" /></ElementType></Schema>