XML Elements vs Attributes

Best practices for deciding when to use child elements versus tag attributes in your schemas.

Interactive Tool: Practice in real-time with Pixlint's free browser-based XML Formatter. No data is uploaded.
Open tool →

One of the most frequent design dilemmas in XML is whether data should be represented as an attribute or a child element:

<!-- Using an attribute -->
<person id="101" name="Sarah" />

<!-- Using child elements -->
<person>
  <id>101</id>
  <name>Sarah</name>
</person>

Rule of Thumb

Use attributes for metadata about the data (e.g. IDs, languages, dates, currency types) that does not constitute the primary content. Use elements for the data itself, especially when the value might contain multiple words, line breaks, or future child elements.