2012-07-29 64 views
7

我試圖驗證對與MSXML 6.0 DOM .xsd文件,但在執行的代碼我收到此錯誤信息的.xml文件:MSXML VBA:針對XSD驗證XML:「提供的'名稱空間與模式的目標名稱空間不同。」

​​

的.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> 

爲了驗證我使用這個代碼用VBA編寫XML文件(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小時沒有積極的結果。

任何幫助將不勝感激。

回答

11

嘗試將名稱空間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 
+0

謝謝Tomalak。在將代碼的某些部分調整爲我的代碼之後,它現在就像一個魅力一樣。 :) – 2012-07-29 14:48:03

+0

@DavidGrant很高興聽到! – Tomalak 2012-07-29 15:09:33

+0

+1爲一個很好的答案。 – 2012-07-30 05:32:00

1

您不應該在xml/xsd中更改任何內容,因爲它們在一起有效。問題出在你的代碼中,所以我建議看看this sample,它顯示我認爲不同於你的代碼:當你將XSD添加到緩存中時,使用目標命名空間http://somewhere.com/root而不是空字符串。

+0

謝謝Petru。你指出問題的本質是正確的。 :) – 2012-07-29 14:51:24