错误:元素模式的名称空间必须来自模式名称空间http://www.w3.org/2001/XMLSchema

时间:2016-07-28 15:53:45

标签: xml xsd xsd-validation xml-validation

以下是我的XSD。我收到了错误。你可以验证这个吗?

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://api.vvz.com/"
            xmlns:vz="http://api.vvz.com/"
            targetNamespace="http://api.vvz.com/">
  <vz:element name="Account">
    <annotation>
      <documentation>
        A sample element
      </documentation>
    </annotation>
    <simpleType name="ID">
    <restriction base="xs:string">
     <pattern value='[a-zA-Z0-9]'/>
    </restriction>
   </simpleType>
     <complexType>
    <complexContent>
     <sequence>
     <element minOccurs="0" maxOccurs="unbounded" name="fieldsToNull"
              nillable="true" type="string"/>
     <element minOccurs="0" maxOccurs="1" name="Id" nillable="true"
              type="vz:ID"/>
    </sequence>
    </complexContent>
   </complexType>
  </vz:element>
</xsd:schema>

错误是

  

元素架构的名称空间必须来自架构命名空间&#39; http://www.w3.org/2001/XMLSchema&#39;

请帮帮我。

2 个答案:

答案 0 :(得分:2)

您对目标命名空间的即时错误是由于在xmlns="http://api.vvz.com/"上声明xsd:schema。删除它。您不希望XSD本身在该命名空间中;您希望该命名空间中受管理的XML,并且已经通过targetNamespace="http://api.vvz.com/"实现了。

XSD的其余部分存在许多错误和目标不明确。这是一套一致的修复,使其有效:

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:vz="http://api.vvz.com/"
            targetNamespace="http://api.vvz.com/">

  <xsd:element name="Account" type="vz:AccountType">
    <xsd:annotation>
      <xsd:documentation>
        A sample element
      </xsd:documentation>
    </xsd:annotation>
  </xsd:element>

  <xsd:complexType name="AccountType">
    <xsd:sequence>
      <xsd:element minOccurs="0" maxOccurs="unbounded" name="fieldsToNull"
                   nillable="true" type="xsd:string"/>
      <xsd:element minOccurs="0" maxOccurs="1" name="Id" nillable="true"
                   type="vz:IdType"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:simpleType name="IdType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value='[a-zA-Z0-9]'/>
    </xsd:restriction>
  </xsd:simpleType>  

</xsd:schema> 

答案 1 :(得分:0)

有几个问题:

  • 构成模式的所有元素必须位于XML Schema名称空间中(使用xsd前缀)。
  • 指定的简单类型必须是顶级。
  • 对内置类型string的引用也必须位于XML Schema名称空间(xsd前缀)中。
  • 元素complexContent无关紧要。

    <?xml version="1.0" encoding="windows-1252" ?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://api.vvz.com/"
        xmlns:vz="http://api.vvz.com/"
        targetNamespace="http://api.vvz.com/">
        <xsd:simpleType name="ID">
            <xsd:restriction base="xsd:string">
                <xsd:pattern value='[a-zA-Z0-9]'/>
            </xsd:restriction>
        </xsd:simpleType>
        <xsd:annotation>
            <xsd:documentation>
                A sample element
            </xsd:documentation>
        </xsd:annotation>
        <xsd:element name="Account">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element minOccurs="0" maxOccurs="unbounded" name="fieldsToNull"
                        nillable="true" type="xsd:string"/>
                    <xsd:element minOccurs="0" maxOccurs="1" name="Id" nillable="true"
                        type="vz:ID"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    
相关问题