Locally Declared Elements and Attributes

Contact Us or call 1-877-932-8228
Locally Declared Elements and Attributes

Locally Declared Elements and Attributes

By default, locally declared elements and attributes in an instance document do not need to be qualified. This can be changed in the schema by including the elementFormDefault and attributeFormDefault attributes of the xs:schema element with the value of "qualified".

First, let's look at the ArtistLocal.xsd schema, which has been modified to make all elements but the Artist element locally declared.

Code Sample:

Namespaces/Demos/ArtistLocal.xsd
<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.webucator.com/Artist" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns="http://www.webucator.com/Artist" 
	elementFormDefault="unqualified" 
	attributeFormDefault="unqualified">
	<xs:element name="Artist">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Name">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Title" type="xs:string"/>
							<xs:element name="FirstName" type="xs:string"/>
							<xs:element name="LastName" type="xs:string"/>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
			<xs:attribute name="BirthYear" type="xs:gYear" use="required"/>
		</xs:complexType>
	</xs:element>
</xs:schema>

Notice that the elementFormDefault and attributeFormDefault attributes are set to "unqualified". This is the default value, so the attributes could just have well been left out.

Now let's take a look at an instance document of this schema.

Code Sample:

Namespaces/Demos/MichaelJacksonLocal.xml
<?xml version="1.0"?>
<art:Artist BirthYear="1958"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:art="http://www.webucator.com/Artist"
	xsi:schemaLocation="http://www.webucator.com/Artist ArtistLocal.xsd">
	<Name>
		<Title>Mr.</Title>
		<FirstName>Michael</FirstName>
		<LastName>Jackson</LastName>
	</Name>
</art:Artist>

When using unqualified locals, it is not valid to use a default namespace. The schema processor must know that these elements are locally declared within a specific namespace. If a default namespace were used, the schema processor would not be able to differentiate between locally declared and globally declared elements. Therefore, we use the art: prefix to qualify the Artist namespace.

Next