XSD Elements Only


An "elements-only" complex type element is an element that can only contain other elements.


Complex type containing elements only

The XML element, "person", contains only other elements:

<person>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</person>

You can define the "person" element in the schema like this:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Please note this. It means that the elements defined must appear in the "person" element in the order shown above.

Alternatively, you can give the complexType element a name and have the "person" element's type attribute reference this name (using this method, several elements can reference the same complex type):

<xs:element name="person" type="persontype"/>

<xs:complexType name="persontype">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Other extensions