XML Schema验证报错‘Invalid schema or missing namespace’求助
XML Schema验证报错"Invalid schema or missing namespace"问题解决
核心错误分析
你的Schema存在多处语法结构错误,并非单纯命名空间问题,这些错误导致验证工具无法正确解析Schema,进而抛出命名空间相关的误导性报错:
- 大小写拼写错误:所有
<xsd:complextype>应为<xsd:complexType>(首字母大写T) - 结构层级错误:不能直接在
<xsd:element>内定义顶级<xsd:complexType>,需将类型定义独立出来或使用匿名嵌套;同时<xsd:simpleType>不能直接作为<xsd:complexType>的子元素,必须通过<xsd:element>引用 - 命名空间前缀不一致:Schema中使用
xsd:作为XML Schema前缀,但限制规则里写成了xs:string,需统一为xsd:string - 属性误用:
use是元素(<xsd:element>)的属性,不能加在<xsd:simpleType>上 - 元素名称不匹配:XML实例中的元素名称(如
email、facebook、middlename)与Schema定义的e-mail、facebookID、middleName不一致
修正后的Schema文件
<?xml version="1.0" encoding="UTF-8" ?> <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.w3.org/ns/data/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- 定义contactInfo复杂类型 --> <xsd:complexType name="contactInfo"> <xsd:sequence> <xsd:element name="phone" type="xsd:string" minOccurs="0"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="30"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="mobile" type="xsd:string" minOccurs="0"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="30"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="e-mail" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="facebookID" type="xsd:string" minOccurs="0"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- 定义personalInfo复杂类型 --> <xsd:complexType name="personalInfo"> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="30"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="middleName" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="30"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="lastName" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="30"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="gender" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="1"/> <xsd:pattern value="F|M"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- 定义addressInfo复杂类型 --> <xsd:complexType name="addressInfo"> <xsd:sequence> <xsd:element name="street" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="50"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="number" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="50"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="city" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="50"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="country" type="xsd:string"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:length value="50"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- 根元素定义 --> <xsd:element name="companyDataModel"> <xsd:complexType> <xsd:sequence> <xsd:element name="contactInfo" type="contactInfo"/> <xsd:element name="personalInfo" type="personalInfo"/> <xsd:element name="addressInfo" type="addressInfo"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
修正后的测试XML文件
<?xml version="1.0" encoding="UTF-8"?> <companyDataModel xmlns="http://www.w3.org/ns/data/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/ns/data/ 你的Schema文件路径.xsd"> <contactInfo> <phone>12345</phone> <mobile>54321</mobile> <e-mail>test@gmail.com</e-mail> <facebookID>fbid</facebookID> </contactInfo> <personalInfo> <firstname>Anna</firstname> <lastName>Karenina</lastName> <middleName>Hanns</middleName> <gender>F</gender> </personalInfo> <addressInfo> <street>Wall Street</street> <number>25</number> <city>New York</city> <country>United States</country> </addressInfo> </companyDataModel>
验证与导入注意事项
- 验证时需确保XML中的
xsi:schemaLocation正确指向Schema文件的实际路径 - 若要将该Schema导入到其他Schema中,需使用
<xsd:import namespace="http://www.w3.org/ns/data/" schemaLocation="你的Schema文件路径.xsd"/>,导入的Schema需与当前Schema在同一命名空间或正确指定命名空间
内容的提问来源于stack exchange,提问作者Noah
相关产品推荐
相关产品推荐

