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

标签 xml vba dom xsd msxml

我正在尝试使用 MSXML 6.0 DOM 针对 .XSD 文件验证 .XML 文件,但在执行代码时我收到此错误消息:

Test.xsd#/schema/targetNamespace[1]
"The '' namespace provided differs from the schema's 'http://somewhere.com/root' targetNamespace."

.XML 和 .XSD 文件的高度简化版本也会产生相同的错误:

XML 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns2:noderoot xmlns:ns2="http://somewhere.com/root">
    <general>
        <year>2011</year>
        <month>02</month>
    </general> 
</ns2:noderoot>

XSD 文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://somewhere.com/root" 
            targetNamespace="http://somewhere.com/root" 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            elementFormDefault="unqualified" 
            attributeFormDefault="unqualified">

    <xs:complexType name="TYPE_nodeGeneral">
        <xs:sequence>
            <xs:element name="year">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:length value="4"/>
                        <xs:pattern value="\d{4}"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="month">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:length value="2"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>           

    <xs:complexType name="TYPE_noderoot">
        <xs:sequence>
            <xs:element name="general" type="TYPE_nodeGeneral"></xs:element>            
        </xs:sequence>
    </xs:complexType>

    <xs:element name="noderoot" type="TYPE_noderoot"></xs:element>

</xs:schema>

为了验证 XML 文件,我使用了这段用 VBA (Excel 2010) 编写的代码:

Sub XSD_Validation()

   XML_FILE = "I:\Test.xml"    
   XSD_FILE = "I:\Test.xsd"

    Dim xmlDoc As MSXML2.DOMDocument60
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    xmlDoc.resolveExternals = False

    xmlDoc.Load XML_FILE

    ' Open XSD file
    Dim obXSD As MSXML2.DOMDocument60
    Set objXSD = New MSXML2.DOMDocument60
    objXSD.async = False
    objXSD.Load XSD_FILE

    ' Populate schema cache
    Dim objSchemaCache As XMLSchemaCache60
    Set objSchemaCache = New MSXML2.XMLSchemaCache60
    objSchemaCache.Add "", objXSD

    ' XSD XML Bind
    Set xmlDoc.Schemas = objSchemaCache

    'Error visualization
    Dim objErr As MSXML2.IXMLDOMParseError
    Set objErr = xmlDoc.Validate()
    If objErr.errorCode <> 0 Then
        Debug.Print "Error parser: " & objErr.errorCode & "; " & objErr.reason
    Else
        Debug.Print "No errors found"
    End If

    Set objErr = Nothing
    Set objXSD = Nothing
    Set objSchemaCache = Nothing
    Set xmlDoc = Nothing

End Sub

可以修改 XSD 文件,但 XML 文件必须保持不变。

我已经尝试解决这个问题 8 个多小时,但没有任何积极结果。

任何帮助将不胜感激。

最佳答案

尝试将命名空间 URI 添加到模式缓存。

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

关于xml - MSXML VBA : Validating XML against XSD: "The ' ' namespace provided differs from the schema's targetNamespace.“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708492/

相关文章:

android - AutoResizeTextView 未按预期工作

vba - 如何获取支持的语言列表、将语言 ID 映射到名称以及在 PowerPoint 演示文稿中显示语言选择器?

php - 遍历DOM向后查找ID

javascript - 使用 javascript 扩展 DOM 范围以覆盖部分选定的节点

sql - XML 输出在 SQL 中被截断

php - simple_xml - 将变量分配给标签 - 仅首先返回

python - 使用 BeautifulSoup 的不同 XML 元素名称列表

date - VBA 日期过滤变成美国格式

复制工作表时 VBA 错误 '9' 下标超出范围

javascript - Backbone.js 为什么 DOM 在渲染函数中不可用?