XSD validation error: Element '{http://www.exa

2019-02-21 21:11发布

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

3条回答
够拽才男人
2楼-- · 2019-02-21 21:24

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-02-21 21:27

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-21 21:40

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.

查看更多
登录 后发表回答