SyntaxStudy
Sign Up
XML Beginner 1 min read

Element Content Models

Every XML element has a content model that describes what it may contain. There are four kinds of content: element content (only child elements, no mixed text), mixed content (text interspersed with child elements), empty content (no children and no text), and any content (anything permitted). Understanding these models is crucial both for designing XML vocabularies and for writing correct DTDs and schemas that enforce them. Element-only content is the cleanest model for data-centric XML, where the structure is hierarchical and text appears only in leaf elements. Mixed content is common in document-centric XML such as XHTML, where text flows around inline elements like or . Empty elements communicate their meaning entirely through attributes or their position in the hierarchy — examples include HTML's
and equivalents in XML vocabularies. The order and cardinality of child elements also matters. In XML Schema, you can specify that a child element must appear exactly once, optionally, one-or-more times, or zero-or-more times using minOccurs and maxOccurs. In a DTD these constraints are expressed using content model operators: comma for sequence, pipe for choice, question mark for optional, asterisk for zero-or-more, and plus for one-or-more.
Example
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Demonstrating the four XML content models
-->
<examples>

    <!-- 1. ELEMENT CONTENT: only child elements, no direct text -->
    <person>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <age>28</age>
    </person>

    <!-- 2. MIXED CONTENT: text and child elements interleaved -->
    <paragraph>
        This is <emphasis>very</emphasis> important and
        <link href="http://example.com">click here</link> for details.
    </paragraph>

    <!-- 3. EMPTY ELEMENT: no content whatsoever -->
    <pageBreak/>
    <image src="photo.jpg" width="800" height="600"/>

    <!-- 4. ANY CONTENT: unconstrained (avoid in strict schemas) -->
    <freeform>
        Text, <child>elements</child>, and <b>anything</b> goes.
        42 <nested><deep>value</deep></nested>
    </freeform>

</examples>