2009-10-14 80 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仅在内部使用,因此不存在破坏现有客户端的风险。