XSD Elements-Only
Elements containing only child elements
🧱 What are Elements-Only Types?
Elements-only content contains child elements but no text. This creates structured hierarchies where parent elements organize child elements without mixing in text content between them.
<!-- Elements-only example -->
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
</person>
Defining Elements-Only Content
Elements-only types use sequences, choices, or groups to define child elements. No text is allowed directly inside the parent element, only structured child elements that organize the data.
🔸 Basic Elements-Only Structure
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
</person>
🔹 Nested Elements-Only Structure
You can create deep hierarchies where parent elements contain child elements, which themselves contain only elements. This creates organized, structured data perfect for complex documents.
<xs:element name="company">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="street" type="xs:string" />
<xs:element name="city" type="xs:string" />
<xs:element name="zip" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<company>
<name>Tech Corp</name>
<address>
<street>123 Main St</street>
<city>Boston</city>
<zip>02101</zip>
</address>
</company>
🔹 Elements with Attributes
Elements-only types can include attributes on the parent element while still containing only child elements, no text:
<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="year" type="xs:integer" />
</xs:sequence>
<xs:attribute name="isbn" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Valid XML:
<book isbn="978-0-123456-78-9">
<title>Learning XML</title>
<author>Jane Smith</author>
<year>2024</year>
</book>
🔹 Complete Example: Product Catalog
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="product" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:decimal" />
<xs:element name="category" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML:
<catalog>
<product id="P001">
<name>Laptop</name>
<price>999.99</price>
<category>Electronics</category>
</product>
<product id="P002">
<name>Mouse</name>
<price>29.99</price>
<category>Accessories</category>
</product>
</catalog>
🔹 Key Characteristics
Structured Data
Organizes information hierarchically
No Text Content
Parent contains only elements
Repeatable
Child elements can repeat
Attributes Allowed
Parent can have attributes