我在尝试根据XML模式验证XML文件时遇到问题。当我尝试验证时,报告的错误是这个
Cannot find the declaration of element 'tns:FlightMonitor'
我不知道我必须更改什么,XML模式似乎没有在XML文件中正确读取或引用。
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<tns:FlightMonitor xmlns:tns="http://www.example.org/FDSInfo/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
tns:schemaLocation="http://www.example.org/FDSInfo/ FDSInfo.xsd">
<Aircrafts>
<aircraft>
<model>E170</model>
<seats>
<seat>12A</seat>
<seat>12B</seat>
<seat>12C</seat>
<seat>13A</seat>
<seat>13B</seat>
<seat>13C</seat>
<seat>14A</seat>
<seat>14B</seat>
<seat>14C</seat>
</seats>
</aircraft>
<aircraft>
<model>A747</model>
<seats>
<seat>12A</seat>
<seat>12B</seat>
<seat>12C</seat>
<seat>13A</seat>
<seat>13B</seat>
<seat>13C</seat>
<seat>14A</seat>
<seat>14B</seat>
<seat>14C</seat>
</seats>
</aircraft>
</Aircrafts>
<Flights>
<flightReader>
<flightNumber>OV876</flightNumber>
<departure>TRN</departure>
<time>
<hour>15</hour>
<minute>30</minute>
</time>
<destination>LTN</destination>
</flightReader>
<flightReader>
<flightNumber>OO076</flightNumber>
<departure>MPX</departure>
<time>
<hour>9</hour>
<minute>22</minute>
</time>
<destination>TRN</destination>
</flightReader>
<flightReader>
<flightNumber>PV276</flightNumber>
<departure>TRN</departure>
<time>
<hour>20</hour>
<minute>00</minute>
</time>
<destination>MPX</destination>
</flightReader>
<flightReader>
<flightNumber>OF376</flightNumber>
<departure>LTN</departure>
<time>
<hour>13</hour>
<minute>10</minute>
</time>
<destination>MPX</destination>
</flightReader>
</Flights>
<FlightInstances>
<flightInstanceReader>
<flightID>OV876</flightID>
<aircraftID>E170</aircraftID>
<date>14/2</date>
<delay>0</delay>
<departureGate>T12</departureGate>
<status>BOARDING</status>
<Passengers>
<passengerReader>
<name>Ivano Alvino</name>
<seatID>12A</seatID>
<boarded>true</boarded>
</passengerReader>
<passengerReader>
<name>Maria Ingenerf</name>
<seatID>12B</seatID>
<boarded>true</boarded>
</passengerReader>
<passengerReader>
<name>Giorgio Napolitano</name>
<seatID>12C</seatID>
<boarded>true</boarded>
</passengerReader>
</Passengers>
</flightInstanceReader>
<flightInstanceReader>
<flightID>PV276</flightID>
<aircraftID>B747</aircraftID>
<date>14/3</date>
<delay>0</delay>
<departureGate>T13</departureGate>
<status>BOARDING</status>
<Passengers>
<passengerReader>
<name>Alfonso Signorini</name>
<seatID>12A</seatID>
<boarded>true</boarded>
</passengerReader>
<passengerReader>
<name>Geronimo Stilton</name>
<seatID>12B</seatID>
<boarded>true</boarded>
</passengerReader>
<passengerReader>
<name>Cecchi Paone</name>
<seatID>12C</seatID>
<boarded>true</boarded>
</passengerReader>
</Passengers>
</flightInstanceReader>
</FlightInstances>
</tns:FlightMonitor>
XML架构
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/FDSInfo"
xmlns:tns="http://www.example.org/FDSInfo" >
<!-- Simple Types Declaration -->
<!-- ************************ -->
<simpleType name="stringType">
<restriction base="string" />
</simpleType>
<simpleType name="modelType">
<restriction base="string">
<pattern value="[A-Z][0-9][0-9][0-9]" />
</restriction>
</simpleType>
<simpleType name="seatType">
<restriction base="string">
<pattern value="[0-9][0-9][A-Z]" />
</restriction>
</simpleType>
<simpleType name="flightNumberType">
<restriction base="string">
<pattern value="[A-Z][A-Z][0-9][0-9][0-9]" />
</restriction>
</simpleType>
<simpleType name="hourType">
<restriction base="integer">
<minInclusive value="0" />
<maxInclusive value="23" />
</restriction>
</simpleType>
<simpleType name="minuteType">
<restriction base="integer">
<minInclusive value="0" />
<maxInclusive value="59" />
</restriction>
</simpleType>
<simpleType name="delayType">
<restriction base="integer"/>
</simpleType>
<simpleType name="statusType">
<restriction base="string">
<enumeration value="ARRIVED"/>
<enumeration value="BOARDING"/>
<enumeration value="BOOKING"/>
<enumeration value="CANCELLED"/>
<enumeration value="CHECKINGIN"/>
<enumeration value="DEPARTED"/>
</restriction>
</simpleType>
<!-- Aircraft -->
<!-- ******** -->
<complexType name="seatsType">
<sequence>
<element name="seat" maxOccurs="unbounded" type="tns:seatType" />
</sequence>
</complexType>
<complexType name="aircraftType">
<sequence>
<element name="model" type="tns:modelType"/>
<element name="seats" type="tns:seatsType"/>
</sequence>
</complexType>
<complexType name="AircraftsType">
<sequence>
<element name="aircraft" maxOccurs="unbounded" type="tns:aircraftType" />
</sequence>
</complexType>
<!-- Flights -->
<!-- ******* -->
<complexType name="timeType">
<sequence>
<element name="hour" type="tns:hourType" />
<element name="minute" type="tns:minuteType" />
</sequence>
</complexType>
<complexType name="flightReaderType">
<sequence>
<element name="flightNumber" type="tns:flightNumberType" />
<element name="departure" type="tns:stringType" />
<element name="time" type="tns:timeType" />
<element name="destination" type="tns:stringType" />
</sequence>
</complexType>
<complexType name="FlightsType">
<sequence>
<element name="flightReader" maxOccurs="unbounded" type="tns:flightReaderType" />
</sequence>
</complexType>
<!-- FlightInstances -->
<!-- *************** -->
<complexType name="passengerReaderType">
<sequence>
<element name="name" type="tns:stringType" />
<element name="seatID" type="tns:seatType" />
<element name="boarded" type="boolean" />
</sequence>
</complexType>
<complexType name="PassengersType">
<sequence>
<element name="passengerReader" maxOccurs="unbounded" type="tns:passengerReaderType" />
</sequence>
</complexType>
<complexType name="flightInstanceReaderType">
<sequence>
<element name="flightID" type="tns:flightNumberType" />
<element name="aircraftID" type="tns:modelType" />
<element name="date" type="date" />
<element name="delay" type="tns:delayType" />
<element name="departureGate" type="tns:stringType" />
<element name="status" type="tns:statusType" />
<element name="Passengers" type="tns:PassengersType" />
</sequence>
</complexType>
<complexType name="FlightInstancesType">
<sequence>
<element name="flightInstanceReader" maxOccurs="unbounded" type="tns:flightInstanceReaderType" />
</sequence>
</complexType>
<!-- FlightMonitor -->
<!-- *************** -->
<complexType name="FlightMonitorType">
<sequence>
<element name="Aircrafts" type="tns:AircraftsType" />
<element name="Flights" type="tns:FlightsType" />
<element name="FlightInstances" type="tns:FlightInstancesType" />
</sequence>
</complexType>
<!-- Root -->
<!-- **** -->
<element name="FlightMonitor" type="tns:FlightMonitorType" />
</schema>
解决了这个问题,我更改了XML文件的第一行
<?xml version="1.0" encoding="UTF-8"?>
<tns:FlightMonitor xmlns:tns="http://www.example.org/FDSInfo/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
tns:schemaLocation="http://www.example.org/FDSInfo/ FDSInfo.xsd">
至
<?xml version="1.0" encoding="UTF-8"?>
<tns:FlightMonitor xmlns:tns="http://www.example.org/FDSInfo/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/FDSInfo/ FDSInfo.xsd ">
并且还更改了中XML模式的第一行
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/FDSInfo"
xmlns:tns="http://www.example.org/FDSInfo" >
至
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/FDSInfo/"
targetNamespace="http://www.example.org/FDSInfo/">