Declaring Global Simple-Type Elements

Contact Us or call 1-877-932-8228
Declaring Global Simple-Type Elements

Declaring Global Simple-Type Elements

When an element declaration is a child of the xs:schema element, the declared element is global. Global elements can be referenced by other element declarations, allowing for element reuse. Take a look at the following example.

Code Sample:

SimpleTypes/Demos/AuthorGlobal.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="FirstName" type="xs:string"/>
	<xs:element name="LastName" type="xs:string"/>
	<xs:element name="Author">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="FirstName"/>
				<xs:element ref="LastName"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

In this example, the FirstName and LastName elements are both declared globally. The global elements are then referenced as children of the Author sequence.

Global vs. Local Simple-Type Elements

The major advantage of declaring an element globally is that the element can then be referenced throughout the schema. This makes the code more modular and easier to maintain. For example, suppose that the song schema contained MusicWriter, LyricsWriter, and Singer elements. Each of these elements might have the child element Name. If the Name element is declared globally, any changes to that element can be made in one location.

The major disadvantage of declaring elements globally is that all global elements must have unique names.

Code Sample:

SimpleTypes/Demos/BookLocal.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="PersonTitle">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Mr."/>
			<xs:enumeration value="Ms."/>
			<xs:enumeration value="Dr."/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Title" type="PersonTitle"/>
							<xs:element name="Name" type="xs:string"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Notice that there are two elements named Title, which can appear in different locations in the XML instance and are of different types. When the Title element appears at the root of the XML instance, its value can be any string; whereas, when it appears as a child of Author, its value is limited to "Mr.", "Ms.", or "Dr."

The example below defines a similar content model; however, because the elements are declared globally, the name Title cannot be used twice.

Code Sample:

SimpleTypes/Demos/BookGlobal.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="PersonTitle">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Mr."/>
			<xs:enumeration value="Ms."/>
			<xs:enumeration value="Dr."/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="BookTitle" type="xs:string"/>
	<xs:element name="Title" type="PersonTitle"/>
	<xs:element name="Name" type="xs:string"/>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="BookTitle"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element ref="Title"/>
							<xs:element ref="Name"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next