tags:

views:

103

answers:

2

Hello everyone,

I am using the following schema to check the following XML file. And I find when there is more than one Information elements inside People elements, the schema check will fail. Why and how to fix it (I want to allow People element to be able to nest more than one Information items)?

XML Schema file:

  <xs:element name="People">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Information">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

XML file (schema check will fail):

  <People>
    <Information Id="1">
      <Name>John</Name>
    </Information>
    <Information Id="2">
      <Name>Mike</Name>
    </Information>
  </People>

XML file (schema check will success):

  <People>
    <Information Id="1">
      <Name>John</Name>
    </Information>
  </People>

thanks in advance, George

+5  A: 

If you dont specify minOccurs and maxOccurs with the sequence, the default value is 1.

<xs:element name="Information" minOccurs = "1" maxOccurs = "unbounded">
Mork0075
Thanks Mork0075, your fix works. I want to confirm that by default, if not specifying min/max occur, the element is allowed to exist once and only once?
George2
Yes, the default for both, min and maxoccurs is 1. And thats in prosa "exists exactly once and only once".
Mork0075
@Mork0075, cool question answered.
George2
+3  A: 
<xs:element name="People">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="unbounded" name="Information">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

Try this it will work for sure

Prashant
Thanks Prashant, your fix works. I want to confirm that by default, if not specifying min/max occur, the element is allowed to exist once and only once?
George2