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

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 validation using 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 one should be correct: the XML file is located in the http://www.example.com namespace (which the validator also says).

And for XSD, I set the default namespace to the XSD schema (this is what Eclipse does, so it should be right!), And I give the correct targetNamespace name. 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?

DIVB Relations

+6
source share
3 answers

An alternative to @ dbasemans answer would be to specify 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 may be considered generic, so you may need to modify your schema as suggested by dbaseman.

+3
source

You must set the targetNamespace and the root XSD namespace to the same value if you do not want to specify any qualifier in the XML file that needs to be checked. So this should 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 have to qualify the XSD elements with xsd :. In other words, to check your XML file as is, you need to write a schema as follows:

 <?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 information: http://www.xfront.com/DefaultNamespace.pdf

EDIT Thanks to PetruGardea for pointing out the error. As follows from Philbert’s answer, elementFormDefault is not qualified by default, which means that the instance documents are supposed to be in the target namespace. So, Philbert's answer is correct - the only alternative would be to make everything anonymous, omitting targetNamespace and leaving elementFormDefault as unqualified, and then completely removing the namespace name from the instance document.

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

+2
source

I found another solution to this problem if you cannot or do not want to modify XSD. The following XML matches your XSD:

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

If elementFormDefault set to be unqualified, you need to define a namespace for global elements, and you must not define a namespace for local elements. Global elements are those that are directly below the schematic element in XSD, and local elements are those that are nested in other elements. Your error was caused by defining a namespace for the local Scope element using the default namespace.

At http://www.oracle.com/technetwork/articles/srivastava-namespaces-092580.html .

+1
source

Source: https://habr.com/ru/post/914871/


All Articles