DTD Basics  «Prev 

Working with XML Mixed Content

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"/>

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
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.
The example above demonstrates mixed content.

The element declaration for REVIEW in the previous example could be written REVIEW like this. In other words, the element can contain any number of PRODUCT-NAME elements or parsed character data, in any order.

When an element is going to contain #PCDATA and child elements, #PCDATA must be listed first with other elements listed after.