23

I am trying to resolve this issue but could not understand the root cause of this error:

Invalid Content Was Found Starting With Element 'country'. One Of '{country}' Is Expected.. Line '10', Column '14'

Here is my 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>

and xsd file for this is:

<?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>
4

1 回答 1

39

如所写,您的架构期望“全局”countries和元素位于命名空间中name,但“本地”元素(在 a 中声明的元素,即和)不在命名空间中。您可能想要添加,即cityhttp://localhost:8080/ajaxpraccomplexTypecountrycitieselementFormDefault="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于本地以及全局元素声明。

于 2013-06-18T16:08:40.800 回答