🔡 XSD Elements-only – Define XML Elements with Only Child Elements (No Text)
🧲 Introduction – Why Use Elements-only Content in XSD?
In XML Schema (XSD), elements-only content refers to elements that are allowed to contain only other elements—not mixed text or attributes. This is useful when modeling structured, predictable data, such as a <book>
with <title>
, <author>
, and <price>
. Enforcing elements-only content improves validation clarity and helps maintain clean XML hierarchies.
🎯 In this guide, you’ll learn:
- What elements-only content is in XSD
- How to define it using
<xs:complexType>
and<xs:sequence>
- Real-world examples of strict structure enforcement
- Best practices and validation rules
📘 What Is Elements-only Content?
Elements-only content means:
- The element can contain zero or more child elements
- The element must not contain any text content or mixed content
- It can optionally include attributes (if needed)
🧾 Syntax – Define Elements-only with Sequence
<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>Jane</firstName>
<lastName>Doe</lastName>
</person>
❌ Invalid:
<person>Jane Doe</person> <!-- Contains text content, not allowed -->
🔁 Allow Repeating Child Elements
You can still define elements that repeat using minOccurs
and maxOccurs
.
🔹 Example – List of Books
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
✅ Valid XML:
<library>
<book>Book One</book>
<book>Book Two</book>
</library>
🧠 Add Attributes to Elements-only Content
<xs:element name="user">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
✅ Combines child elements with metadata via attributes.
❌ Mixed Content vs Elements-only
Type | Allows Child Elements | Allows Text | Use For |
---|---|---|---|
Elements-only | ✅ Yes | ❌ No | Structured data (books, users) |
Mixed | ✅ Yes | ✅ Yes | Markup content (HTML-like XML) |
✅ Best Practices for Elements-only Content
- ✔️ Use
<xs:sequence>
to define child element order - ✔️ Avoid
xs:simpleContent
ormixed="true"
to keep it strict - ✔️ Combine with attributes for metadata
- ❌ Don’t include text between tags unless switching to mixed content
- ❌ Don’t define empty child elements unless necessary—structure should reflect purpose
📌 Summary – Recap & Next Steps
Elements-only content ensures XML elements are structured and contain only other elements. This pattern is ideal for modeling clean, nested XML formats without unexpected text values—perfect for databases, APIs, and structured data feeds.
🔍 Key Takeaways:
- Use
<xs:complexType>
with<xs:sequence>
to enforce elements-only - No text is allowed—only nested tags
- Attributes may still be added
⚙️ Real-world relevance: Commonly used in XML for e-commerce catalogs, APIs, data exports, and configuration formats.
❓ FAQs – XSD Elements-only
❓ Can I include text inside elements-only content?
❌ No. Only child elements are allowed.
❓ What if I want to allow both text and child elements?
✅ Use mixed content instead (see XSD Mixed
).
❓ Can elements-only content have attributes?
✅ Yes. Attributes can be declared inside the <xs:complexType>
.
❓ What structure enforces elements-only?
✅ Use <xs:complexType>
with <xs:sequence>
, <xs:choice>
, or <xs:all>
and no mixed="true"
.
❓ Can I make child elements optional or repeatable?
✅ Yes. Use minOccurs
and maxOccurs
on each child declaration.
Share Now :