Search code examples
xmlxsdschemavalidationxmllint

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


Solution

  • 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.