Search code examples
xmlxsdsocial-networking

define an element as a list of another element in xml schema


I want to define a smaple XML Schema for a social network website.I've defined a Person element and it's related attributes and other child elements.it's a part of the schema which I've defined. :

<xs:element name="Person" type="Person" />
    <xs:complexType name="Person">
        <xs:sequence>
            <xs:element name="firstName" type="xs:string"/>
            <xs:element name="lastName" type="xs:string"/>
            <xs:element name="nickName" type="xs:string"/>
            <xs:element name="age" >
                <xs:simpleType>
                    <xs:restriction base="xs:integer">
                      <xs:minInclusive value="0"/>
                      <xs:maxInclusive value="120"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="birthDate" type="xs:date"/>
            <xs:element name="gender" type="Gender" />
            <xs:element name="contactDetails" type="ContactInfo"/>
            <xs:element name="occupation" type="Occupation" /> 
        </xs:sequence>
</xs:complexType>

now I want to define another child for Person as it's friends but I dont know how can I do this.does any body have any Idea about this?


Solution

  • Does this work for you:

      <xs:element name="Persons">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Person" type="PersonType" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="uniquePersonId">
          <xs:selector xpath="./Persons"/>
          <xs:field xpath="@PersonID"/>
        </xs:unique>
      </xs:element>
    
      <xs:complexType name="PersonType">
        <xs:sequence>
          <xs:element name="firstName" type="xs:string"/>
          <xs:element name="lastName" type="xs:string"/>
          <xs:element name = "Friends" minOccurs="0" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Friend" minOccurs="0" maxOccurs="unbounded">
                  <xs:complexType >
                    <xs:attribute name="FriendId" type="xs:IDREF" use="required" />
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
        <xs:attribute name="PersonID" type="xs:ID" use="required"/>
      </xs:complexType>