среда, 11 сентября 2019 г.

Валидация XML по XSD: ошибка "No matching global declaration available for the validation root."

Пример ошибочно сформированного XML-файла, валидация которого приводит к ошибке:
No matching global declaration available for the validation root.

XSD:
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Devices" xmlns:tns="http://www.example.org/Devices"  elementFormDefault="qualified">
   <element name="devices">
      <complexType>
         <sequence>
            <element name="computerDevice"
                     type="tns:computerDevice"
                     minOccurs="3"
                     maxOccurs="unbounded"/>
         </sequence>
      </complexType>
   </element>
<complexType name="computerDevice">
   <sequence>
      <element name="name" type="string"/>
      <element name="origin" type="string"/>
      <element name="price" type="tns:Range"/>
      <element name="type" type="tns:Type"/>
      <element name="critical" type="tns:Choice"/>
   </sequence>
<attribute name="id" type="ID" use="required"/>
</complexType> 
<simpleType name="Range">
     <restriction base="integer">
       <minInclusive value="0"/>
       <maxInclusive value="1000"/>
     </restriction>
</simpleType>
<complexType name="Type">
   <sequence>
      <element name="peripheral" type="tns:Choice"/>
      <element name="energy_consumption" type="int"/>
      <element name="cooler" type="tns:Choice"/>
      <element name="group" type="string"/>
      <element name="port">
         <simpleType>
            <restriction base="string">
               <enumeration value="HDMI"></enumeration>
               <enumeration value="COM"></enumeration>
               <enumeration value="LPT"></enumeration>
            </restriction>
         </simpleType>
      </element>
   </sequence>
</complexType>
<simpleType name="Choice">
   <restriction base="string">
      <pattern value="Yes|No|yes|no"/>
   </restriction>
</simpleType>
</schema>

XML (с ошибкой):
<?xml version="1.0" encoding="UTF-8"?>
<devices>
    <computerDevice id = "d1">
        <name>Keyboard</name>
        <origin>Chine</origin>
        <price>10</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>2</energy_consumption>
            <cooler>No</cooler>
            <group>Input</group>
            <port>USB</port>
        </type>
        <critical>No</critical>
    </computerDevice>
    <computerDevice id = "d2">
        <name>Monitor</name>
        <origin>Japanese</origin>
        <price>17</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>20</energy_consumption>
            <cooler>No</cooler>
            <group>Output</group>
            <port>HDMI</port>
        </type>
        <critical>Yes</critical>
    </computerDevice>
    <computerDevice id = "d3">
        <name>printer</name>
        <origin>Azerbaijan</origin>
        <price>14</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>50</energy_consumption>
            <cooler>No</cooler>
            <group>Output</group>
            <port>COM</port>
        </type>
        <critical>No</critical>
    </computerDevice>
</devices>

XML (исправленный - см. строку №2):
<?xml version="1.0" encoding="UTF-8"?>
<devices xmlns="http://www.example.org/Devices">
    <computerDevice id = "d1">
        <name>Keyboard</name>
        <origin>Chine</origin>
        <price>10</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>2</energy_consumption>
            <cooler>No</cooler>
            <group>Input</group>
            <port>USB</port>
        </type>
        <critical>No</critical>
    </computerDevice>
    <computerDevice id = "d2">
        <name>Monitor</name>
        <origin>Japanese</origin>
        <price>17</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>20</energy_consumption>
            <cooler>No</cooler>
            <group>Output</group>
            <port>HDMI</port>
        </type>
        <critical>Yes</critical>
    </computerDevice>
    <computerDevice id = "d3">
        <name>printer</name>
        <origin>Azerbaijan</origin>
        <price>14</price>
        <type>
            <peripheral>Yes</peripheral>
            <energy_consumption>50</energy_consumption>
            <cooler>No</cooler>
            <group>Output</group>
            <port>COM</port>
        </type>
        <critical>No</critical>
    </computerDevice>
</devices>

Источник.

Комментариев нет:

Архив