Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML/XSD Validation

Status
Not open for further replies.

jerasi

Programmer
Jun 19, 2000
141
CA
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=&quot;x-schema:XQL&quot; xmlns:dt=&quot;urn:schemas-microsoft-com:datatypes&quot;><DATE dt:dt=&quot;datetime&quot;>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=&quot;1.0&quot;?><Schema xmlns=&quot;urn:schemas-microsoft-com:xml-data&quot;><ElementType name=&quot;TITLE&quot; /><ElementType name=&quot;AUTHOR&quot; /><ElementType name=&quot;PUBLISHER&quot; /><ElementType name=&quot;DATE&quot; /><ElementType name=&quot;BOOK&quot; model=&quot;closed&quot;><element type=&quot;TITLE&quot; /><element type=&quot;AUTHOR&quot; /><element type=&quot;PUBLISHER&quot; /></ElementType><ElementType name=&quot;COLLECTION&quot; model=&quot;closed&quot;><element type=&quot;BOOK&quot; /></ElementType></Schema>
 
Hi Jerasi,

From a previous post I learned that you use the MSXML 4.0 parser. As far as I know adding schema references at run time to existing xmldata wasn't possible with the MSXML 3.0 SP 1 parser and I'm not sure if it is possible at all.

You see, you add the Schema reference after the load, but a schema is needed during a load!

Just add the schemareference to the xml, set the value of the domdocument.validateonparse property to true and your xmldocument will be validated on load.

If an error occures use the domdocument.parseerror collection to learn more.

Good luck!

Jordi Reineman
 
I tried your suggestion but, I'm still getting the error &quot;Invalid at the top level of the document&quot; (after the XML has loaded correctly) do you see anything wrong with the XML or XSD documents?

Thanks
Jerasi.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top