Jolta
Jolta

Reputation: 2725

XML validation gives validation error, but I can't find the cause

I've tried to design an XML schema, but I keep getting stuck. My XML file won't validate against my schema. I suspect that I'm making some beginner's mistake. Can anyone point out where I'm going wrong? The error message doesn't make sense to me.

Here's my xsd file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.com/teststuff/XMLnamespace" >
    <xs:element name="dataCollecFile">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="fileHeader" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="data" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="fileFooter">
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
</xs:schema>

And here's the content of the XML file that won't validate:

<?xml version="1.0" encoding="UTF-8"?> 
<dataCollecFile xmlns="http://www.test.com/teststuff/XMLnamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com/teststuff/XMLnamespace test.xsd">
   <fileHeader>heady head header</fileHeader>
   <data>data data</data>
   <fileFooter>foot feet feeter</fileFooter>
</dataCollecFile>

Finally, here's the error message that I get from Java's javax.xml.validation.Validator class:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'fileHeader'. One of '{fileHeader, data, fileFooter}' is expected.

Upvotes: 1

Views: 3959

Answers (1)

forty-two
forty-two

Reputation: 12817

Try adding elementFormDefault="qualified" to your <schema> element.

UPDATE

When elementFormDefault is unqualified, global elements will hav the target name space, but local elements will hav no namespace. So, if you had the unqualified version of the schema, your XML would have to look something like this:

<ns:dataCollecFile xmlns:ns="http://www.test.com/teststuff/XMLnamespace" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.test.com/teststuff/XMLnamespace test.xsd">
    <fileHeader>heady head header</fileHeader>
    <data>data data</data>
    <fileFooter>foot feet feeter</fileFooter>
</ns:dataCollecFile>

Upvotes: 3

Related Questions