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

3

8

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

Infraction answered 4/5, 2012 at 8:8 Comment(1)
Typo? tnd:Scopeshould be tns:ScopeBaronage
B
4

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.

Baronage answered 4/5, 2012 at 8:26 Comment(4)
Hi, thank you (to both of you)! The one thing I do not understand then: For what do I need targetNamespace then?Infraction
Actually, your answer is correct (+1) as opposed to the other one, which is wrong. However, many people would argue that the xsd could be considered as a standard prefix - I would use "common" instead. In fact, xs is the one defined in the DTD that typically accompanies the XSD for XSD...Typo
@Infraction You still need targetNamespace unless you want to hide (localize) the example.com namespace in the instance document.Loner
"xsd:" is Microsoft and "xs:" is the one used in the XML Schema specification ergo "xsd:" is definitely not the standard (even though some people assume that Microsoft = Standard).Hemianopsia
L
2

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

Loner answered 4/5, 2012 at 8:21 Comment(1)
Unfortunately, your answer has nothing to do with the question; there's no problem with the XSD, and what you're proposing is not changing the schema in any way; your updated XSD does not change the outcome of validating the proposed XML.Typo
M
1

I found another solution to this problem, if you can't or don't want to change the XSD. The following XML conforms to your XSD:

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

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

There are further explanations at http://www.oracle.com/technetwork/articles/srivastava-namespaces-092580.html.

Mauk answered 21/3, 2016 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.