views:

206

answers:

1

Hi all,

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  A: 

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"/>
Eddie
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.
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...
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
Yes, excelent, it works great. Many thanks for the support!!!
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.
Eddie
@Jozef: If you try to get xs:unique to work and cannot, then you can always create a new question (rather than ask for help in comments). That will allow people to create detailed answers.
Eddie