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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate XML to XSD with MSXML 1

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
0
0
GB
I'm trying to validate an XML against the XSD Schema.
At the point of adding the SchemaCache I get the error Incorrect definition for the root element in schema.

I may be completely wrong but I maybe missing a required namespace. I've having trouble understanding the MSXML model and I'm struggling to understand what I need here.

I'd be thankful for any help.

Code:
	XML_FILE = "XML File Path" 
	XSD_FILE = "XSD File Path"
    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    xmlDoc.resolveExternals = False

    xmlDoc.Load XML_FILE

    ' Open XSD file
    Set objXSD = CreateObject("MSXML2.DOMDocument")
    objXSD.async = False
    objXSD.Load XSD_FILE

    ' Populate schema cache
    Set objSchemaCache = CreateObject("MSXML2.XMLSchemaCache")

    [b][red]objSchemaCache.Add "", objXSD[/red][/b]
 
    ' XSD XML Bind
    Set xmlDoc.Schemas = objSchemaCache
 
    'Error visualization
    Set objErr = xmlDoc.Validate()
    If objErr.errorCode <> 0 Then
        sResult = "Error parser: " & objErr.errorCode & "; " & objErr.reason
    Else
        sResult = "No errors found"
    End If
	XSD_Validation = sResult

The XML header example:
Code:
<ftc:FATCA_OECD xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns="urn:oecd:ties:fatca:v1" xmlns:ftc="urn:oecd:ties:fatca:v1" xmlns:iso="urn:oecd:ties:isofatcatypes:v1" 
xmlns:sfa="urn:oecd:ties:stffatcatypes:v1" version="1.1">
 
Try using

Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
Set objXSD = CreateObject("MSXML2.DOMDocument.6.0")
Set objSchemaCache = CreateObject("MSXML2.XMLSchemaCache.6.0")

although I'd probably not bother with objXSD at all, and simply add the schema directly:

objSchemaCache.Add "<your_schemas_namespace>", XSD_FILE
 
Strongm,
Thanks for replying. I'm not sure I fully understand the concept of NameSpaces. As per the header in the OP would I need include multiple NameSpaces or just first?
Also without the objXSD Wharf anything would be the second parameter fir the .Add method?
 
Thanks again, I'll try it when I'm back in the office.
 
Many thanks, "urn:eek:ecd:ties:fatca:v1" was indeed the namespace and specifying .6.0 ref has has given a fuller script error message:

FatcaXML_v1.1.xsd#/schewma/complexType[4][@name= 'CorrectableOrganisationParty_Type']/complexContent[1]/extention[1]Undefined
Undefined<complexType>,
'{urn:eek:ecd:ties:stffatcatypes:v1}OrganisationParty_Type' is used as a base type.

code: 80004005
source: msxml6.dll

Unfortunately I'm still at a loss as to the cause / resolution of this. Is this an error with the xml data (it has previously schema validated using xmlSpy), or is there still a problem with the code?
 
I seem to have solved it, and it was thanks to strongm : "although I'd probably not bother with objXSD at all, and simply add the schema directly"

Lessons learned: listen to any advice given, in full.
 
Difficult to say without the XSD and a (theoretically) valid XML example
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top