验证XML-Schema(xsd)错误

时间:2014-05-01 15:42:07

标签: xml netbeans schema xsd

我一直收到此错误,但我无法弄清楚代码中的错误(我使用的是NetBeans IDE 6.7.1:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'manufacturer'.  One of '{manufacturer}' is expected. [14] 
cvc-complex-type.2.4.a: Invalid content was found starting with element 'manufacturer'. One of '{manufacturer}' is expected. [23] 
cvc-complex-type.2.4.a: Invalid content was found starting with element 'manufacturer'. One of '{manufacturer}' is expected. [32] 
XML validation finished.

这是我的XML代码:

<?xml version="1.0" encoding="UTF-8"?>
<jobList xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com jobList.xsd">

<job status="unallocated">
    <resource employeeId="AD145267" type="Laptop">
        <manufacturer>Dell</manufacturer>
        <model>Vostro 1540</model>
        <serialNumber>764839211-19-H</serialNumber>
    </resource>
    <problem reported="2012-01-22T15:23:00">Fails to boot</problem>
</job>

<job status="unallocated">
    <resource employeeId="TE135218" type="Wireless printer">
        <manufacturer>Epson</manufacturer>
        <model>Stylus SX435W</model>
        <serialNumber>E4356-982312</serialNumber>
    </resource>
    <problem reported="2012-01-23T10:07:00">No wireless access</problem>
</job>

<job status="allocated">
    <resource type="Network drive">
        <manufacturer>G-Tech</manufacturer>
        <model>G-RAID Hard drive array</model>
        <serialNumber>783451287G</serialNumber>
    </resource>
    <problem reported="2012-01-24T11:02:00">Read errors reported during rebuild</problem>

    <work jobId="564">
        <technician allocated="2012-01-24T15:05:00">T541</technician>
        <note>Spoke to Eric about what happened.</note>
        <note>Ran phase 1 diagnostics - no errors reported</note>
    </work>
</job>

</jobList>

这是架构代码

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3schools.com"   targetNamespace="http://www.w3schools.com">

<xs:element name="jobList">
<xs:complexType>
  <xs:sequence minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="job"/>
  </xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="job">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="resource"/>
    <xs:element ref="problem"/>
    <xs:sequence minOccurs="0">
      <xs:element ref="work"/>
    </xs:sequence>
  </xs:sequence>
  <xs:attribute name="status" use="required">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="unallocated"/>
        <xs:enumeration value="allocated"/>
        <xs:enumeration value="completed"/>
        <xs:enumeration value="confirmed"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
</xs:complexType>
</xs:element>

<xs:element name="resource">
<xs:complexType>
  <xs:sequence>
    <xs:element name="manufacturer" type="xs:string"/>
    <xs:element name="model" type="xs:string"/>
    <xs:element name="serialNumber" type="xs:string"/>
  </xs:sequence>
  <xs:attribute name="employeeId" type="xs:string"/>
  <xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="problem">
<xs:complexType>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="reported" type="xs:dateTime" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:element name="work">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="technician"/>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="note"/>
    </xs:sequence>
  </xs:sequence>
  <xs:attribute name="jobId" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="technician">
<xs:complexType>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="allocated" type="xs:dateTime" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="note" type="xs:string"/>
</xs:schema>

它关注制造商,我正在努力寻找错误。我还将链接相关的DTD代码,因为额外的信息是有用的。

<!ELEMENT resource (manufacturer, model, serialNumber)>
<!ELEMENT manufacturer (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEME.....

1 个答案:

答案 0 :(得分:0)

您的架构希望找到<manufacturer>无命名空间。例如:

<manufacturer xmlns="">...</manufacturer>

但实际上您希望它与{"http://www.w3schools.com":manufacturer}(来自您的目标命名空间)进行验证。要将该行为声明为所有元素的默认行为,请将elementFormDefault="qualified"添加到XML Schema声明中:

<xs:schema ... elementFormDefault="qualified"> ... </xs:schema>