XSD Mixed Content
Elements with both text and child elements
🔀 What is Mixed Content?
Mixed content allows elements to contain both text and child elements interleaved together. This is common in document-oriented XML like HTML, where text and markup coexist naturally.
<!-- Mixed content example -->
<paragraph>
This is <bold>important</bold> text.
</paragraph>
Defining Mixed Content
Mixed content is defined using the mixed="true" attribute on complexType. This allows text to appear anywhere between child elements, creating flexible document structures.
🔸 Basic Mixed Content
<xs:element name="paragraph">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="bold" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<paragraph>
This is <bold>important</bold> text with <bold>multiple</bold> highlights.
</paragraph>
🔹 Rich Text Example
Mixed content is perfect for rich text formatting where you need to mix plain text with inline formatting elements like bold, italic, and links.
<xs:element name="description">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="bold" type="xs:string" />
<xs:element name="italic" type="xs:string" />
<xs:element name="link">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="href" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
Valid XML:
<description>
Visit our <link href="https://example.com">website</link> for
<bold>amazing</bold> deals and <italic>special</italic> offers!
</description>
🔹 Document-Style Content
Mixed content is essential for document-oriented XML where narrative text contains embedded markup elements:
<xs:element name="article">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="content">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="emphasis" type="xs:string" />
<xs:element name="code" type="xs:string" />
<xs:element name="quote" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<article>
<title>Learning XML</title>
<content>
XML is <emphasis>powerful</emphasis> for data exchange.
Use <code><xs:element></code> to define elements.
As they say, <quote>practice makes perfect</quote>.
</content>
</article>
🔹 Complete Example: Blog Post
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="post">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="body">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="strong" type="xs:string" />
<xs:element name="em" type="xs:string" />
<xs:element name="a">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="href" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML:
<post>
<title>Getting Started with XSD</title>
<author>John Developer</author>
<body>
Welcome to this <strong>comprehensive guide</strong> on XSD.
You'll learn <em>everything</em> you need to know.
Check out <a href="https://w3.org">W3C</a> for more info.
</body>
</post>
🔹 When to Use Mixed Content
Documents
Articles, books, reports
Rich Text
Formatted descriptions
HTML-like Content
Web page content
Messages
Emails, comments, posts