0

I am having an issue with xsd schema. I have a following schema defined:

<xs:element name="nodes" type="nodesRootType" />

<xs:complexType name="nodesRootType">
    <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element name="node" type="nodeType" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="nodeType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="node" type="nodeType" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>

and I am testing it on following xml:

<label name="Label_6" >
    <label name="Label_7" />
</label>
<label name="Label_8" />

The error I am getting is :

cvc-complex-type.2.4.a: Invalid content 
    was found starting with element 'node'. One of '{node}' is expected.

What am I missign here. Any help is hightly appreciated.

Best, Jozef

2
  • You might want to edit your question, I can't see the schema nor the xml. Commented Feb 11, 2009 at 15:07
  • I've updated my answer. I can correctly serialize an XML of the form you want. There are two parts to the fix: 1) moving maxOccurs and minOccurs to the correct location, and 2) defining the element separately, in one location, so it's obviously the same type at both schema locations.
    – Eddie
    Commented Feb 11, 2009 at 18:33

1 Answer 1

3

A single XML document that you are verifying cannot have multiple roots. It must have a single root. Your XML:

<label name="Label_6">
    <label name="Label_7"/>
</label>
<label name="Label_8"/>

has two roots because there are two "label" elements both at the root level. Also, your schema does not define any element named "label" so I am confused. You probably want a schema that will validate something like the following XML:

<labels>
    <label name="Label_6">
        <label name="Label_7"/>
    </label>
    <label name="Label_8"/>
</labels>

where here you have a single root "labels". Note that you have elements with the same name, "label" at different levels in the hierarchy. This isn't verboten (see, for example, xhtml where you can have a div inside a div inside ...) but it can be confusing. For the sample XML shown above, try a schema something like:

<xs:element name="labels">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="label" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:complexType name="labelType">
  <xs:sequence>
    <xs:element ref="label" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="label" type="labelType"/>
6
  • Yes, I cut the xml example out of context. Sorry for that. You are correct, I want to validate exactly what you have written. You said that I do not have 'label' element in the schema. Can you please provide a schema example for xml stated above? Many thanks.
    – Jozef
    Commented Feb 11, 2009 at 17:58
  • It is throwing same error. I found by trials that if I replace <xs:element name="label" type="labelType"/> with <xs:element ref="label"/> in "recursive" complexType definition, it works ... I just do not know why is that...
    – Jozef
    Commented Feb 11, 2009 at 18:19
  • I found your problem. You have the minOccurs and maxOccurs in the wrong location. Check out my edit above. With this schema and sample XML, I am able to correctly serialize and deserialize.
    – Eddie
    Commented Feb 11, 2009 at 18:29
  • Hi Eddie, may I ask you one more thing? Is it possible to check with schema some unique constrains? Let's say I want to have a name attribute of label element a unique name cross whole xml. Is it possible? Thanks in advance.
    – Jozef
    Commented Feb 12, 2009 at 20:32
  • @Jozef: Yes, use xs:unique to require uniqueness of values. Check out books.google.com/…
    – Eddie
    Commented Feb 12, 2009 at 22:04