找到无效的内容从元素'country'开始.其中一个'{country}'预计..行'10',列'14'

nwz*_*der 23 xsd xml-parsing

我试图解决此问题,但无法理解此错误的根本原因:

找到无效的内容从元素'country'开始.其中一个'{country}'预计..行'10',列'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>
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

Ian*_*rts 37

由于写的,你的架构预计,"全球性" countries,name以及city元素要在http://localhost:8080/ajaxprac命名空间,但"本地"元素(这些声明的内部complexType,即countrycities)是在没有命名空间.你可能想要添加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">
Run Code Online (Sandbox Code Playgroud)

它适用targetNamespace于本地和全局元素声明.

  • 谢谢,你救了我的一天.现在我可以下班回家了.:) (3认同)