VBA: Validate XML against XSD - MSXML2 Undeclared Element?

ironfelix717

Registered User.
Local time
Today, 08:02
Joined
Sep 20, 2019
Messages
193
Hi all,

I am attempting validation of a XML (KML) via MSXML2 against a cached copy of the official XSD

Here is validation code. The runtime error I receive complains about an 'undeclared XSD element' and then the atom namespace in the schema/file.

The KML validates against the XSD with web based 3rd party tools. Perhaps MSXML2's validation proc is just another 'great work' of MS :LOL:
Either way, I am inquiring about how to resolve this issue, so I can check the validation of this file.

Heres the sub, a sample KML file can be downloaded on google for testing...

Thanks in advance for looking


Code:
Sub validateXMLDoc()
       'Schema
            Dim SCHEMA_LOCATION As String
            Dim SCHEMA_NAMESPACE As String
            Dim XML_LOCATION As String
           
           
            'enter inputs here:
            XML_LOCATION = "\\Mac\home\desktop\KML_samples.kml"
            SCHEMA_LOCATION = "\\Mac\Home\Desktop\Schema.xsd"
            SCHEMA_NAMESPACE = "http://www.opengis.net/kml/2.2"

           
           
            Dim objSchemaCache As Object
            Set objSchemaCache = New XMLSchemaCache60
           
            objSchemaCache.Add SCHEMA_NAMESPACE, SCHEMA_LOCATION
     
            Dim objerr As MSXML2.IXMLDOMParseError2
         
       'XML Document
            Dim xmlDoc As MSXML2.DOMDocument60
            Set xmlDoc = New MSXML2.DOMDocument60
            Set xmlDoc.schemas = objSchemaCache
            xmlDoc.async = False ': xmlDoc.validateOnParse = False
            xmlDoc.Load (XML_LOCATION)
         
            With xmlDoc
                .SetProperty "MultipleErrorMessages", True
                Set objerr = .Validate()
            End With
            Dim oErr As Object
        'Error code : Reason along with Line Number
            For Each oErr In objerr.allErrors
                MsgBox "Error parser: " & " " & oErr.line & oErr.errorCode & "; " & oErr.reason
            Next oErr
       
            Set xmlDoc = Nothing
    End Sub


Related links:
 
I noticed that you have yet to receive a reply to your question, hence I am bumping it up the list so that it gets another look. Let's hope you get lucky...!!!
 
Way beyond my level. Hope you get an answer soon...
 
@Uncle Gizmo
I was unable to solve this issue validating against an XSD.
The closest I came was by removing the atom namespace and all the atom elements, within the XSD. The code then at least did not throw any runtime errors. However, the validation did not pass saying 'multiple root items in document' basically. Which is incorrect. The KML is valid XML and is read by KML applications such as Google Earth with no problems.


With that said, I find using the XMLSchemaCache60 to be a waste at this point, but would be curious to learn from an XML+VBA guru on why i'm not getting a passing validation. Or furthermore, why do I have to hand edit an XSD to get a running validation?
MSXML is obsolete and ancient, unfortunately like many of the tools we were left with.

So, what is the solution? I overshot the problem entirely. I guess, all i really need to know is how to how to check for XML syntax errors. And that can be done with the example below:

Code:
Dim xDoc        As New MSXML2.DOMDocument60

xDoc.async = False
xDoc.validateOnParse = True
xDoc.Load ("\\Mac\Home\Desktop\photokml.kml")

If xDoc.parseError.reason = "" Then
    Debug.Print "OK"
Else
Debug.Print "STATUS: " & xDoc.parseError.reason
End If


Will keep open for the time being.
Thanks a lot
 

Users who are viewing this thread

Back
Top Bottom