XML
Beginner
1 min read
Attributes vs Child Elements — Design Decisions
Example
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--
ATTRIBUTE-heavy style: metadata as attributes
Good for: identifiers, flags, simple scalars
-->
<product id="p001" category="electronics" inStock="true" price="299.99">
<name>Wireless Headphones</name>
</product>
<!--
ELEMENT-heavy style: everything as child elements
Good for: structured data, repeating data, data that may grow
-->
<product>
<id>p002</id>
<category>electronics</category>
<inStock>true</inStock>
<price currency="USD">149.99</price>
<name>Bluetooth Speaker</name>
<!-- address could not be a simple attribute value -->
<manufacturer>
<name>SoundCo</name>
<country>USA</country>
</manufacturer>
<!-- Tags can repeat — impossible with attributes -->
<tag>audio</tag>
<tag>portable</tag>
<tag>bluetooth</tag>
</product>
</catalog>