Attributes vs child elements: modelling decisions

When data belongs in an attribute, why attributes cannot repeat or nest, how mixed content changes the rules, and a review of a small schema design.

What each form can express

PropertyAttributeChild element
RepeatsNo, one value per nameYes, any number of siblings
OrderNot significantSignificant and preserved
Nested structureImpossibleArbitrary depth
Mixed with textNeverYes, this is the point of mixed content
NamespaceMust carry its own prefix, never inherits the defaultInherits the default namespace
WhitespaceNormalised by the parserPreserved unless the schema says otherwise
Schema validationSimple types onlyFull datatypes, including lists and sequences
Human readabilityCompact, good for metadataVerbose but self-describing
<?xml version="1.0" encoding="UTF-8"?>
<!-- attributes for identifying metadata, elements for content -->
<article id="a-1024" lang="en" status="published" version="3">
  <title>Parsing XML without surprises</title>
  <author role="primary">
    <name>Ana P.</name>
    <email>[email protected]</email>
  </author>
  <author role="reviewer">
    <name>Bo K.</name>
  </author>
  <tags>
    <tag>xml</tag>
    <tag>parsing</tag>
  </tags>
</article>

The id, language, status and version identify the article and never repeat. The authors and tags repeat, so they are elements. That single question — does this value ever repeat, and does it ever contain structure — settles most modelling decisions.

Mixed content and the text problem

When an element contains both text and child elements, XML requires a clear model, because whitespace around the children is data in some formats and formatting in others.

<p>The <em>first</em> rule is to escape <code>&lt;</code> as <code>&amp;lt;</code>.</p>

<!-- the same content with everything as elements: workable, but loses the prose -->
<para>
  <text>The </text>
  <em>first</em>
  <text> rule is to escape </text>
  <code>&lt;</code>
  <text> as </text>
  <code>&amp;lt;</code>
  <text>.</text>
</para>
  • Mixed content is appropriate for documents and prose, where the markup is interleaved with the text.
  • For data records, avoid mixed content entirely: a field either holds a value or holds children, not both.
  • An xml:space="preserve" attribute asks an application to keep whitespace, but it is a hint and not all processors honour it.
  • Pretty-printing a document with mixed content changes the text, which is why round-tripping a formatted document is risky.

Reviewing a design

BEFORE                              AFTER

<order id="1" date="2026-09-18"      <order id="1">
       customer="Ada" total="19.99">    <placedAt>2026-09-18</placedAt>
  <item sku="A1" qty="2"/>            <customer>
  <item sku="B2" qty="1"/>              <name>Ada</name>
</order>                                <email>[email protected]</email>
                                      </customer>
attribute holds a nested record       <items>
attribute repeats per item             <item sku="A1" qty="2"/>
(impossible, so it is flattened)        <item sku="B2" qty="1"/>
                                      </items>
                                      <total currency="EUR">19.99</total>
                                    </order>
  1. Can the value repeat? If yes, it must be an element.
  2. Does it contain structure? If yes, it must be an element.
  3. Is it identifying metadata with a small, fixed vocabulary? A good attribute candidate.
  4. Would a human read it in a log or a diff? Attributes are denser and easier to scan.
  5. Does it need to participate in a default namespace? Elements only.
  6. Will the schema need to restrict it with facets? Element with a named simple type, or an attribute with the same restriction.
💡
The cost of the wrong choice is paid later. A repeated fact squeezed into an attribute forces a concatenated string that every consumer must split, and a metadata flag promoted to an element adds noise to every document — both are hard to change once published.

FAQ

Are attributes faster to parse?
Marginally, because they are flat. The difference is negligible next to the document size, and it is never a good reason to model repeating data as an attribute.
Can an attribute hold a list?
Only as a space-separated string that the schema cannot validate as a list unless the type is declared as a list. Elements are clearer and validate properly.

XML Schema in practice: reuse and evolution RSS, Atom and web feeds

Last refreshed 2026-09-18.