Complex elements
Where simple elements only contain data, complex elements can hold much more. Complex elements can contain attributes, child elements, and additional indicators that focus on the order of child elements and how often they can occur. Let’s look at an example of a complex element:
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="isbn" type="xs:integer" />
<xs:element name="genre" type ="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Exiting code block.
The opening tag indicates the name of the element we’re declaring, and the following tag indicates that this element is a complex element. The next tag indicates that the child elements we include should be included in a specific order - the following table explains the different order indicators we can choose from:
Finally, the child elements are declared. There are two ways to indicate a complex element’s children: we can either declare the element inside the complex element, as shown in the previous example, or refer to a previously declared simple element, like in the following example:
<xs:element ref="title" />
Exiting code block.
Additionally, if using the sequence order indicator, we can indicate how often the elements can occur inside of the parent element using maxOccurs, which indicates the maximum number of times the element can appear, and minOccurs, which indicates the minimum number of times an element can appear. The occurrence indicator is included at the end of the element declaration, as seen in the following example:
<xs:element name="genre" type="xs:string" maxOccurs="3" />
Exiting code block.
NOTE: To indicate that an element can appear an unlimited number of times, the value for maxOccurs should be set to unbounded.
At this point, we’re ready to declare the complex elements we’ll be using. We'll be declaring <job>
and making references to all the child elements it contains, which we declared previously as simple elements. We'll also be declaring the element <midwest-job-listing>
, and indicating that its one child, <job>
, can be repeated inside this element an unlimited amount of times.