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

一曲冷凌霜 提交于 2019-12-01 16:25:50

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!