MSXML VBA: Validating XML against XSD: “The '' namespace provided differs from the schema's targetNamespace.”

走远了吗. 提交于 2019-11-30 14:16:11

Try adding the namespace URI to the schema cache.

Sub XSD_Validation()
    Dim xmlDoc As MSXML2.DOMDocument60
    Dim objSchemaCache As New XMLSchemaCache60
    Dim objErr As MSXML2.IXMLDOMParseError

    objSchemaCache.Add "http://somewhere.com/root", LoadXmlFile("I:\Test.xsd")

    Set xmlDoc = LoadXmlFile("I:\Test.xml")
    Set xmlDoc.Schemas = objSchemaCache

    Set objErr = xmlDoc.Validate()
    If objErr.errorCode = 0 Then
        Debug.Print "No errors found"
    Else
        Debug.Print "Error parser: " & objErr.errorCode & "; " & objErr.reason
    End If
End Sub

Function LoadXmlFile(Path As String) As MSXML2.DOMDocument60
    Set LoadXmlFile = New MSXML2.DOMDocument60

    With LoadXmlFile
        .async = False
        .validateOnParse = False
        .resolveExternals = False
        .load Path
    End With
End Function

You shouldn't have to change anything in your xml/xsd, since together they are valid. The problem is in your code, so I would suggest to take a look at this sample that shows what I think is different than your code: when you add the XSD to the cache, use the target namespace http://somewhere.com/root instead of the empty string.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!