Defining Complex Types Globally

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

Contact Us or call 1-877-932-8228
Defining Complex Types Globally

Defining Complex Types Globally

As with simple types, complex types can be defined globally. The example below shows how this is done.

Code Sample:

ComplexTypes/Demos/Author.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:complexType name="Person">
		<xs:sequence>
			<xs:element name="FirstName" type="xs:string"/>
			<xs:element name="LastName" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
	<xs:element name="Author" type="Person"/>
</xs:schema>

As you can see, complex types are defined with the xs:complexType element. The major advantage of defining a complex type globally is that it can be reused. For example, a schema might allow for an Illustrator element as well as an Author element. Both elements could be of type Person. This way, if the Person type is changed later, the change will apply to both elements.

The instance document below will validate properly against the schema above.

Code Sample:

ComplexTypes/Demos/MarkTwain.xml
<?xml version="1.0"?>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="Author.xsd">
	<FirstName>Mark</FirstName>
	<LastName>Twain</LastName>
</Author>
Next

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