DTD Basics  «Prev 

Working with XML Mixed Content

What are "Mixed Content" models in XML?
In XML, "Mixed Content" refers to an element content model that allows both text and child elements within an element. This means that the element can contain both character data (i.e. text content) and child elements in any order. For example, consider an XML element named "paragraph" that can contain both text and a child element named "emphasis":
<!ELEMENT paragraph (#PCDATA | emphasis)*>
<!ELEMENT emphasis (#PCDATA)>

In this example, the "paragraph" element is defined as having mixed content by using the asterisk (*) to indicate that any number of occurrences of either character data or the "emphasis" element can appear in any order. The "emphasis" element is defined as containing only character data.
With this content model, the following are all valid examples of "paragraph" elements:
<paragraph>This is a simple paragraph.</paragraph>

<paragraph><emphasis>This</emphasis> is an <emphasis>important</emphasis> paragraph.</paragraph>

<paragraph><emphasis>This</emphasis> paragraph contains <emphasis>multiple</emphasis> emphases.</paragraph>

Mixed content models can be useful when the content of an element needs to contain both text and other elements, such as in the case of HTML-like markup languages or rich-text document formats. However, they can also make it more difficult to validate and process the XML document, as the order and content of child elements can vary.


XML Mixed Content

Mixed content models enable you to include both text and element content within a single content model. To create a mixed content model in XML Schemas, simply include the mixed attribute with the value true in your <complexType> definition, like so:

<element name="description">
<complexType mixed="true">
<choice minOccurs="0" maxOccurs="unbounded">
<element name="em" type="string"/>
<element name="strong" type="string"/>
<element name="br" type="string"/>
</choice>
</complexType>
</element>

The preceding example declares a <description> element, which can contain an infinite number of <em>, <strong>, and <br> elements. Because the complex type is declared as mixed, text can be interspersed throughout these elements. An allowable <description> element might look like the following:
<description> Joe is a developer 
& author for Beginning XML 5th edition
</description>
In this <description> element, textual content is interspersed throughout the elements declared within the content model. As the schema validator is processing the preceding example, it skips over the textual content and entities while performing standard validation on the elements. Because the elements <em>, <strong>, and <br> may appear repeatedly
(maxOccurs="unbounded"),  
the example is valid.