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

北城以北 提交于 2019-12-01 15:09:23

问题


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


回答1:


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.




回答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




回答3:


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.



来源:https://stackoverflow.com/questions/10445055/xsd-validation-error-element-http-www-example-comscope-this-element-is-n

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