Share via


Named Model Groups

 

The group element is used both for the definition of a group and for any reference to a named group. You can use a model group to define a set of elements that can be repeated through the document. This is useful for building a complex type definition. Named model groups can be further defined by using sequence, choice, or all child elements.

A named model group consists of element declarations, wildcards, and other model groups. Named model groups must have a name attribute and are declared at the top level of the schema.

An unnamed group element is not declared at the top level and must have a ref attribute that references an existing named group. Unnamed groups must not have a name attribute. A named group can be referenced, multiple times, using an unnamed group in the XML Schema document.

Example

The following example creates a named model group, stationeries, that allows a limited set of 1-5 books and 1-5 pens to appear in the group.

<xs:group name="stationeries">
   <xs:choice>
      <xs:element name="books" minOccurs="1" maxOccurs="5" />
      <xs:element name="pens" minOccurs="1" maxOccurs="5" />
   </xs:choice >
</xs:group>

Referencing a Global Group Declaration

You can use the ref attribute to reference a group by name in order to use the group description.

Example

The following example references a model group, stationeries, inside of the choice element.

<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
 
<xs:group name="stationeries">
   <xs:sequence>
      <xs:element name="books" minOccurs="1" maxOccurs="5" />
      <xs:element name="pens" minOccurs="1" maxOccurs="5" />
   </xs:sequence>
</xs:group>

<xs:element name="Order">
   <xs:complexType>
      <xs:choice>
        <xs:element name="table" minOccurs="1" maxOccurs="5" />
        <xs:group ref="stationeries" minOccurs="1" />
      </xs:choice>
   </xs:complexType>
</xs:element>

</xs:schema>