Adding Simple-Type Elements to a Schema - Exercise

Contact Us or call 1-877-932-8228
Adding Simple-Type Elements to a Schema - Exercise

Adding Simple-Type Elements to a Schema

Duration: 10 to 15 minutes.

In this exercise, you will be adding simple-type elements to a schema.

  1. Open SimpleTypes/Exercises/Song.xsd for editing.
  2. Between the open and close xs:sequence tags, declare three new elements:
    • Title of type xs:string.
    • Year of type xs:gYear.
    • Artist of type xs:string.
  3. Save the file.
  4. Try to validate LoveMeDo.xml against the schema you just created. If the XML document is invalid, fix your schema.

Code Sample:

SimpleTypes/Exercises/Song.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Song">
		<xs:complexType>
			<xs:sequence>
				<!--
					Add three simple-type elements:
					1. Title
					2. Year
					3. Artist
				-->
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Solution:

SimpleTypes/Solutions/Song.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Song">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Year" type="xs:gYear"/>
				<xs:element name="Artist" type="xs:string"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next