Adding ComplexType Elements - Exercise

Welcome to our free XML Schema tutorial. This tutorial is based on Webucator's Introduction to XML Schema course.

Contact Us or call 1-877-932-8228
Adding ComplexType Elements - Exercise

Adding ComplexType Elements

Duration: 15 to 25 minutes.

In this exercise, you will modify the song schema, adding model groups and occurrence constraints.

  1. Open ComplexTypes/Exercises/Song.xsd for editing.
  2. Modify the schema so that the Song element's children can appear in any order.
  3. Change the content model of the Song element so that it contains an Artists element in place of the Artist element.
  4. Make the Artists element allow for one or more child Artist elements. Note that the Artist element is already declared globally.
  5. Make the Length element optional.
  6. Try to validate ComplexTypes/Exercises/WeAreTheWorld.xml against the schema you just created. If the XML document is invalid, fix your schema.

Solution:

ComplexTypes/Solutions/Song.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
---- C O D E   O M I T T E D ----

	<xs:element name="Song">
		<xs:complexType>
			<xs:all>
				<xs:element ref="Title"/>
				<xs:element ref="Year"/>
				<xs:element name="Artists">
					<xs:complexType>
						<xs:sequence>
							<xs:element ref="Artist" maxOccurs="unbounded"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element ref="Length" minOccurs="0"/>
			</xs:all>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next

This tutorial is based on Webucator's Introduction to XML Schema Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.