错误:检测到X在名称空间Y中,但来自此名称空间的组件不能从模式文档中引用

问题描述:

此xsd元素有什么问题?错误:检测到X在名称空间Y中,但来自此名称空间的组件不能从模式文档中引用

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:complexType name="MessageInfoType"> 
     <xsd:sequence> 
      <xsd:element minOccurs="0" maxOccurs="1" name="TimeStamp" type="xsd:string" /> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:element name="GetData"> 
     <xsd:annotation> 
      <xsd:documentation>Send data</xsd:documentation> 
     </xsd:annotation> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element minOccurs="1" name="MessageInfo" type="xsd:MessageInfoType" /> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

获取错误未声明MessageInfoType。

该错误消息,

src-resolve.4.2: Error resolving component 'xsd:MessageInfoType'. It was detected that 'xsd:MessageInfoType' is in namespace ' http://www.w3.org/2001/XMLSchema ', but components from this namespace are not referenceable from schema document XSD filename. If this is the incorrect namespace, perhaps the prefix of 'xsd:MessageInfoType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to XSD filename.

当组件引用但在给定的命名空间不发生找到。

对于您的情况,您已通过向type="xsd:MessageInfoType"添加不必要的名称空间前缀以及XSD根元素上不必要的默认名称空间前缀来阻止成功引用MessageInfoType

如何解决:xsd:schema删除默认命名空间声明,并从type="xsd:MessageInfoType"上的MessageInfo声明中删除命名空间前缀:

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema elementFormDefault="qualified" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:complexType name="MessageInfoType"> 
    <xsd:sequence> 
     <xsd:element name="TimeStamp" type="xsd:string" 
        minOccurs="0" maxOccurs="1" /> 
    </xsd:sequence> 
    </xsd:complexType> 
    <xsd:element name="GetData"> 
    <xsd:annotation> 
     <xsd:documentation>Send data</xsd:documentation> 
    </xsd:annotation> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element name="MessageInfo" type="MessageInfoType" 
        minOccurs="1" /> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

那么错误信息会自动消失。