2009-10-14 75 views
-1

我試圖使用IXMLDOMDocument2接口(C++)來驗證XML文檔對某些架構和我得到了以下錯誤:架構驗證錯誤「重複命名<element>:名稱=‘X’」

Duplicate named <element> : name = '{http://www.site.com/MySchema}envelope'. 

我很努力去理解這是什麼意思 - 我的模式有問題,還是這是Xml的問題?我已經檢查了模式和Xml,他們兩次幾乎都沒有包含「信封」兩個字!

的XML:

<id:envelope xmlns:id="http://www.site.com/MySchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.site.com/MySchema MySchema.xsd"> 
    <id/> 
    <!-- Load of unimportant elements --> 
</id:envelope> 

的XSD:

<xsd:schema targetNamespace="http://www.site.com/MySchema" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://www.site.com/MySchema" elementFormDefault="unqualified"> 
    <xsd:element name="envelope" type="envelopeType"> 
     <!-- etc... --> 
    </xsd:element> 
    <xsd:complexType name="envelopeType"> 
     <!-- etc... --> 
    </xsd:complexType> 
    <!-- load of other types... --> 
</xsd:schema> 
+0

那個寂寞的標籤呢?它不屬於MySchema? – 2009-10-14 11:40:34

+0

我認爲它憑藉targetNamespace標籤做了。不是嗎?我省略了大量名稱相似的元素(例如「sender」,而不是「id:sender」),所以我認爲這不是問題(但我不知道:-S) – Justin 2009-10-14 12:16:16

回答

1

我想通了這一點感謝評論留在this page MSDN上的結束:

In MSXML4, schemaLocation and noNamespaceSchemaLocation were never used during validation: you should use a SchemaCache containing the schemas against which the document was validated. This was fine, because it allowed me to use 'local' versions of the schemas that were referenced in the XML document.

In MSXML6, this was changed: "Inline schemas and schemas referenced from an instance using xsi:SchemaLocation are now added to an XML instance-specific cache which wraps the user-supplied SchemaCache." Now, when i use the SchemaCache and add the 'local' version of the schemas that were referenced in the XML document, i get this error message: "Duplicate named : name = 'ROOT'".

It seems both xsi:schemaLocation and the SchemaCache are used during validation resulting in a conflict. Ik know i can use ResolveExternal=False so xsi:schemaLocation won't be used, but in that case xsd:import/xsd:include are not resolved either, so that's not an option.

我發現我可以從輸入xml中刪除schemaLocation屬性,或者不顯式添加MySchema.xsd文檔模式緩存和賦值將成功。

最後,我決定刪除schemaLocation屬性,因爲它保留了現有的行爲--Xml僅在內部使用,因此不存在破壞現有客戶端的風險。