Converting Simple-Type Element Declarations from Local to Global - Exercise

Contact Us or call 1-877-932-8228
Converting Simple-Type Element Declarations from Local to Global - Exercise

Converting Simple-Type Element Declarations from Local to Global

Duration: 10 to 20 minutes.

In this exercise, you will convert the element declarations in the song schema from local to global.

  1. Open SimpleTypes/Exercises/Song3.xsd and save it as Song4.xsd in the same directory.
  2. Change the Title, Year, Artist, and Length elements to be declared globally.
  3. Try to validate SimpleTypes/Exercises/StrawberryFields.xml against the schema you just created. If the XML document is invalid, fix your schema.

Solution:

SimpleTypes/Solutions/Song4.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="ProperName">
		<xs:restriction base="xs:string">
			<xs:whiteSpace value="collapse"/>
			<xs:pattern value="([A-Z0-9][A-Za-z0-9\-']* ?)+"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="Year">
		<xs:restriction base="xs:gYear">
			<xs:minInclusive value="1950"/>
			<xs:maxInclusive value="1970"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="SongLength">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Short"/>
			<xs:enumeration value="Medium"/>
			<xs:enumeration value="Long"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="SongTime">
		<xs:union memberTypes="xs:duration SongLength"/>
	</xs:simpleType>
	<xs:element name="Title" type="ProperName"/>
	<xs:element name="Year" type="Year"/>
	<xs:element name="Artist" type="ProperName"/>
	<xs:element name="Length" type="SongTime"/>
	<xs:element name="Song">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="Title"/>
				<xs:element ref="Year"/>
				<xs:element ref="Artist"/>
				<xs:element ref="Length"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next