我正在尝试解决这个问题,但不能理解这个错误的根本原因:
Invalid Content Was Found Starting With Element ‘country’. One Of ‘{country}’ Is Expected.. Line ’10’,Column ’14’
这是我的xml:
<?xml version="1.0"?> <!--DTD file reference--> <!--<!DOCTYPE countries SYSTEM "http://localhost:8080/ajaxprac/file.dtd">--> <!--DTD file reference--> <!----> <countries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost:8080/ajaxprac" xsi:schemaLocation="http://localhost:8080/ajaxprac fileSchema.xsd"> <country> <name>pakistan</name> <cities> <city>Kassowal</city> <city>Faisalabad</city> <city>Multan</city> </cities> </country> <country> <name>india</name> <cities> <city>Agra</city> <city>Amritsar</city> <city>Ayodhya</city> </cities> </country> </countries>
而xsd文件是:
<?xml version="1.0"?> <!-- To change this template,choose Tools | Templates and open the template in the editor. --> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost:8080/ajaxprac" xmlns="http://localhost:8080/ajaxprac"> <xs:element name="countries" type="countriesType"/> <xs:element name="name" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:complexType name="countriesType"> <xs:sequence> <xs:element name="country" type="countryType"/> </xs:sequence> </xs:complexType> <xs:complexType name="countryType"> <xs:sequence> <xs:element ref="name"/> <xs:element name="cities" type="citiesType"/> </xs:sequence> </xs:complexType> <xs:complexType name="citiesType"> <xs:sequence> <xs:element ref="city"/> </xs:sequence> </xs:complexType> </xs:schema>
如你所愿,您的架构期望“全球”国家,名称和城市元素位于http:// localhost:8080 / ajaxprac命名空间中,而“local”元素(在complexType(即国家和城市)内)没有命名空间。你可能想添加
elementFormDefault="qualified"
,即
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost:8080/ajaxprac" xmlns="http://localhost:8080/ajaxprac" elementFormDefault="qualified">
它将targetNamespace应用于本地以及全局元素声明。