Skip to content

XSD Elements Only

How to define XML elements containing only child elements in XSD using complex types with xs:sequence for ordered child elements.

An elements-only complex type contains an element that contains only other elements.

Complex Types Containing Elements Only

An XML element, "book", that contains only other elements:

<book>
	<title>A Great Book</title>
	<author>Tom Nolan</author>
	<publisher>Tutorial Reference</publisher>
</book>

You can define the "book" element in a schema, like this:

<xs:element name="book">
	<xs:complexType>
		<xs:sequence>
			<xs:element name="title"  type="xs:string"/>
			<xs:element name="author"  type="xs:string"/>
			<xs:element name="publisher"  type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
</xs:element>

Or you can give the complexType element a name, and let the "book" element have a type attribute that refers to the name of the complexType

<xs:element name="book" type="bookinfo"/>

<xs:complexType name="bookinfo">
	<xs:sequence>
		<xs:element name="title"  type="xs:string"/>
		<xs:element name="author"  type="xs:string"/>
		<xs:element name="publisher"  type="xs:string"/>
	</xs:sequence>
</xs:complexType>
Navigation