1
votes

I'm trying to define an .xsd file that will restrict a .xml document to contain certain information. This is the .xsd.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/MicroscopyExperiment"
        xmlns:tns="http://www.example.org/MicroscopyExperiment">
  <element name="MicroscopyExperiment">
    <complexType>
      <sequence>
        <element name="goal" type="string" minOccurs="1" maxOccurs="1"/>
        <element name="cellType" type="string"/>
        <element name="cellDensity" type="string"/>
        <element name="injury" type="string"/>
        <element name="drug" type="string"/>
        <element name="media" type="string"/>
        <element name="timing" type="string"/>
        <element name="coating" type="string"/>
        <element name="plateList">
          <complexType>
             <sequence>
               <element name="plate" 
                        type="tns:plateType" 
                        maxOccurs="unbounded" 
                        minOccurs="1"/>
             </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
  <complexType name="plateType">
    <sequence>
      <element name="goalDiff" type="string"/>
      <element name="cellTypeDiff" type="string"/>
      <element name="cellDensityDiff" type="string"/>
      <element name="injuryDiff" type="string"/>
      <element name="drugDiff" type="string"/>
      <element name="mediaDiff" type="string"/>
      <element name="timingDiff" type="string"/>
      <element name="coatingDiff" type="string"/>
    </sequence>
  </complexType>
</schema>

This .xsd file validates fine in Eclipse Neon.1a Release (4.6.1).

Then I created a first .xml file to validate against this schema. This is the .xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<MicroscopyExperiment
  xmlns:tns="http://www.example.org/MicroscopyExperiment" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd">

</MicroscopyExperiment>

This also validates just fine in Eclipse. I don't get any of the error messages about being unable to find the .xsd file that have been driving me crazy all day. The problem is that the .xml shouldn't validate. I set both minOccurs and maxOccurs for the goal element to be 1, to require that it occur once and only once. However, there is no goal in the .xml file right now so that should fail to validate.

Any tips would be much appreciated.

Regards,

Dessie

1
Can you add your sample XML file? - Rubens Farias

1 Answers

1
votes

You are making a bit of a mess with the namespaces. In the schema, you include a targetNamespace. This means that all globally declared elements belong to that namespace.

In your XML file however, you use MicroscopyExperiment without a namespace prefix. And because you don't declare a default namespace either, this element is not matched with the element declaration in the Schema. One way to solve this is to declare the correct default namespace in the XML file:

<MicroscopyExperiment
    xmlns="http://www.example.org/MicroscopyExperiment"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd">
</MicroscopyExperiment>

Another solution is to add the namespace prefix to the element name:

<tns:MicroscopyExperiment
    xmlns:tns="http://www.example.org/MicroscopyExperiment"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd">
</tns:MicroscopyExperiment>

ADDENDUM: Note that minOccurs="1" and maxOccurs="1" are the default, you don't need to include them in the schema. Also, you probably should add the elementFormDefault="qualified" attribute to the schema element, otherwise local elements (plate) do not belong to any namespace, which will cause more confusion.