Skip to content

XSD Empty Elements

How to define empty complex elements in XSD schemas using attributes without content, including compact syntax and named complex types for reuse.

An empty complex element cannot have contents, only attributes.

Complex Empty Elements

A complex empty XML element, called "product":

<product itemid="1234"/>

To define a type with no content, we must define a type that allows elements in its content, but we do not actually declare any elements, like this:

<xs:element name="product">
	<xs:complexType>
		<xs:complexContent>
			<xs:restriction base="xs:integer">
				<xs:attribute name="itemid" type="xs:positiveInteger"/>
			</xs:restriction>
		</xs:complexContent>
	</xs:complexType>
</xs:element>

However, it is possible to declare the "product" element more compactly, like this:

<xs:element name="product">
	<xs:complexType>
		<xs:attribute name="itemid" type="xs:positiveInteger"/>
	</xs:complexType>
</xs:element>

Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType. In this way, several elements can refer to the same complex type:

<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">
	<xs:attribute name="itemid" type="xs:positiveInteger"/>
</xs:complexType>
Navigation