Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

Document structure

Mixed content

An element containing both text and child elements, interleaved — the model prose uses and data rarely does.

Mixed content is what XML was built for: a paragraph with emphasis inside it, a description with a link in the middle. The text between and around the children is content in its own right, and its position matters.

It is also what makes XML awkward to convert. JSON has no way to express text interleaved with objects, so every XML-to-JSON conversion either loses the interleaving or invents a representation for it. If your documents carry mixed content, check what your converter did before trusting the output.

Defined in
XML 1.0 §3.2.2
Category
Document structure
Also written
text and elements, #PCDATA model

Shown, not asserted

All 3 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.

  • The children include text nodes, not just elements.

    document.xml
    <p>Some <b>bold</b> text.</p>
    expression
    count(/p/node())

    What the engine returns

    3
  • The element's string value is all of the text, in document order.

    document.xml
    <p>Some <b>bold</b> text.</p>
    expression
    string(/p)

    What the engine returns

    Some bold text.
  • A minifier must leave that whitespace alone: dropping it would join words together.

    document.xml
    <p>Some <b>bold</b> text.</p>

    What the engine returns

    minify
    <?xml version="1.0"?>
    <p>Some <b>bold</b> text.</p>
    

What catches people out

  • Whitespace in mixed content is significant. Pretty-printing such a document changes what it says.
  • XML-to-JSON conversion cannot represent interleaving faithfully. Expect the text and the children to be separated.
  • A DTD content model for mixed content can constrain which elements may appear but never their order or number.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.