Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD validation error: Element '{http://www.example.com}Scope': This element is not expected. Expected is ( Scope )

I created the following XSD (with Eclipse):

  <?xml version="1.0" encoding="UTF-8"?>
  <schema targetNamespace="http://www.example.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.com">
    <element name="Make">
      <complexType>
        <sequence>
          <element name="Scope"></element>
        </sequence>
      </complexType>
    </element>
  </schema>

and validating with this simple XML

  <?xml version="1.0"?>
  <Make xmlns="http://www.example.com">
    <Scope>
    </Scope>
  </Make>

gives:

  xmllint.exe --noout --schema sources.xsd sources.xml
  sources.xml:3: element Scope: Schemas validity error : Element '{http://www.example.com}Scope': This element is not expected. Expected is ( Scope ).
  sources.xml fails to validate

In my opinion, this must be correct: the XML file is in the namespace http://www.example.com (what also the validator says).

And for the XSD I set the default namespace to the XSD schema (this is what Eclipse does, so it should be correct!) and I give the correct targetNamespace. I also tried to use

<element name="tnd:Scope" />

However, this does not work either.

Is this a bug in xmllint or where is the problem?

Regards divB

like image 377
divB Avatar asked May 04 '12 08:05

divB


People also ask

What is XSD validation error?

The schema is defined by the XSD. Schema errors occur where there is a problem with the structure or order of the file, or an invalid character is included. Schema errors prevent the validation being run in full because the file cannot be read. This means that errors cannot be traced to a particular record.

How do you make an element required in XSD?

The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use="required" (Note: use may also be set to "prohibited", but we'll come to that later).

What is element in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.


2 Answers

An alternative to @dbasemans answer would be to specify the elementFormDefault as qualified:

 <schema targetNamespace="http://www.example.com"
     xmlns="http://www.w3.org/2001/XMLSchema"
     xmlns:tns="http://www.example.com"
     elementFormDefault="qualified">

Using the xsd or xs prefix for your schema namespace could be considered as common, so may want to choose to modify your schema as suggested by dbaseman.

like image 132
Filburt Avatar answered Oct 28 '22 22:10

Filburt


You have to set both the targetNamespace and the root XSD namespace to the same value, if you don't want to specify any qualifier in the XML file to be validated. So it would have to be:

<schema targetNamespace="http://www.example.com" xmlns="http://www.example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

But then of course, you'd have to qualify the XSD elements with xsd:. In other words, to have your XML file validate as is, you'd need to write the schema like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com">
    <xsd:element name="Make">
        <xsd:complexType>
            <xsd:sequence>
               <xsd:element name="Scope"></xsd:element>
            </xsd:sequence>
       </xsd:complexType>
    </xsd:element>
</xsd:schema>

See here for more info: http://www.xfront.com/DefaultNamespace.pdf

EDIT Thanks to PetruGardea for pointing out the error. As Filbert's answer implies, elementFormDefault is unqualified by default, which means that instance documents are assumed to be in the target namespace. So Filbert's answer is correct-- the only alternative would be to make the whole thing anonymous, by omitting targetNamespace and leaving elementFormDefault as unqualified, and then removing the namespace refernce from the instance document entirely.

Here's a good breakdown of what elementFormDefault does: http://www.xfront.com/HideVersusExpose.html

like image 40
McGarnagle Avatar answered Oct 28 '22 20:10

McGarnagle