Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

This should cover W3C XML Schema, Relax NG and DTD related problems.
Selusar
Posts: 3

Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

Mon May 21, 2012 4:25 pm

Hi all,

I'm a firsttimer with writing XSD's and now I've hit a validation error which I cannot seem to resolve. Hope one of you could help me out.

XML I want to validate:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<DOStreetLightList xmlns="http://munisense.com/webservices/v1/"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://munisense.com/webservices/v1/ muniws.xsd">
   <DOStreetLight id="12">
       <object_id value="2334">32ef:8903:328df</object_id>
      <description error="434" />
      <level error="434" />
   </DOStreetLight>
   <DOStreetLight id="12">
      <object_id value="23214">234:234:as23</object_id>
      <level error="12" />
   </DOStreetLight>
</DOStreetLightList>


Schema:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:muniws="http://munisense.com/webservices/v1/"
   targetNamespace="http://munisense.com/webservices/v1/">

   <xs:element name="DOStreetLightList">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="muniws:DOStreetLight" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:element name="DOStreetLight">
      <xs:complexType>
         <xs:all>
            <xs:element name="object_id" type="muniws:DOProperty" minOccurs="0" />
            <xs:element name="description" type="muniws:DOProperty" minOccurs="0" />
            <xs:element name="level" type="muniws:DOProperty" minOccurs="0" />
         </xs:all>
         <xs:attribute name="id" type="xs:string" use="required" />
      </xs:complexType>
   </xs:element>   

   <xs:complexType name="DOProperty">
      <xs:attribute name="error" use="optional" type="xs:string" />
      <xs:attribute name="value" use="optional" type="xs:string" />
   </xs:complexType>

</xs:schema>


I get the following error:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'object_id'. One of '{object_id, description, level}' is expected. which I found odd because object_id is present in the list, why isn't it recognised?

Thanks in advance for any help.
adrian
Posts: 2461

Re: Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

Mon May 21, 2012 5:11 pm

Hello,

The problem is that your schema hides (localizes) the namespaces (by default), but your XML instance is defined as if they are exposed.
If you find these concepts confusing, there is an explanation and a straightforward example here:
http://www.xfront.com/HideVersusExpose.html

In short, what you have to do to make this work correctly in its current form, is expose the namespaces in the schema. In practice that means you should add the attribute: elementFormDefault="qualified" to the root element of your schema.

Another possibility is to leave the schema as it is and adjust the XML instance to match the current schema. For that you have to bring object_id, description and level in "no namespace". That means you should specify xmlns="" for each of them.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Radu
Posts: 5867

Re: Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

Mon May 21, 2012 5:25 pm

Hi,

In your XML Schema these three elements are declared locally (inside the xs:all):

Code: Select all

<xs:element name="object_id" type="muniws:DOProperty" minOccurs="0"/>
<xs:element name="description" type="muniws:DOProperty" minOccurs="0" />
<xs:element name="level" type="muniws:DOProperty" minOccurs="0" />


According to the XML Schema specification, this means that they are declared to be in "no namespace" instead of the XML Schema's target namespace.

In your XML instance you seem to expect that the "object_id" element should belong to the namespace http://munisense.com/webservices/v1/.

You have several possible fixes:

1) On the xs:schema root element add this additional attribute elementFormDefault="qualified". This way, every locally declared element will be considered to pertain to the schema's target namespace.

2) When declaring the local elements add form="qualified" to them like:

Code: Select all

<xs:element name="object_id" type="muniws:DOProperty" minOccurs="0" form="qualified"/>


form="qualified".

3) Instead of declaring the elements locally, declare them globally.

Usually XML Schema editors use the first option because there are seldom cases in which someone wants to declare a local element in "no namespace".

Please see this part of the specification:

http://www.w3.org/TR/xmlschema-0/#UnqualLocals

Qualification of local elements and attributes can be globally specified by a pair of attributes, elementFormDefault and attributeFormDefault, on the schema element, or can be specified separately for each local declaration using the form attribute. All such attributes' values may each be set to unqualified or qualified, to indicate whether or not locally declared elements and attributes must be unqualified.


Indeed the error given by the Xerces validator is veru cryptic, we'll see if we can improve on this.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Selusar
Posts: 3

Re: Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

Mon May 21, 2012 5:29 pm

Thanks both for the fast reaction! I've added elementFormDefault qualified and the error is gone and replaced by:

cvc-complex-type.2.1: Element 'object_id' must have no character or element information item [children], because the type's content type is empty.

This error disappears when I leave the object ID empty instead.

Code: Select all

<object_id value="2334"/>


Seems that

Code: Select all

<xs:element name="object_id" type="muniws:DOProperty" minOccurs="0" />

forces object_id to be empty but that is not the desired behavior.

Any idea's? Thanks!
Selusar
Posts: 3

Re: Validation problem csvc-complex-type.2.4.a Invalid content - But element is in the list!

Mon May 21, 2012 5:42 pm

Update:

Probleem in post hierboven inmiddels opgelost door het te wrappen in een

Code: Select all

   <xs:complexType name="DOProperty" >
      <xs:simpleContent>
         <xs:extension base="xs:string">
            <xs:attribute name="error" use="optional" type="xs:string" />
            <xs:attribute name="value" use="optional" type="xs:string" />
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>


Ik heb het werkend nu, cheers :)

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 1 guest