XML Elements and Attributes
Estimated study time: 12 minutes.
XML documents are built from two core building blocks: elements and attributes. Knowing when to use each is key to designing clean, maintainable XML.
Elements
An element is a tag (and its content) that represents a piece of data. Elements can contain text, other elements, or both.
<student>
<name>Rahul Verma</name>
<age>24</age>
</student>
Attributes
An attribute provides additional information about an element, written inside its opening tag as a name-value pair.
<student id="102" status="active">
<name>Rahul Verma</name>
</student>
Elements vs Attributes — When to Use Which
- Use an element when the data could have multiple values, sub-structure, or might need to grow over time.
- Use an attribute for simple, single-value metadata about the element itself, like an ID or a status flag.
Combining Both
<course id="DA-101" level="beginner">
<title>Data Analytics Fundamentals</title>
<duration>4 Months</duration>
</course>
Naming Rules Every Document Must Follow
XML is strict. A single naming or nesting error makes the whole document fail to parse, so learn these rules early.
- Names are case-sensitive:
<Name>and<name>are different elements. - Names must start with a letter or underscore, not a digit or punctuation.
- Names cannot contain spaces. Hyphens, dots and underscores are allowed, but avoid colons except for namespaces.
- Names starting with the letters "xml" in any case are reserved.
- Every opening tag needs a closing tag, or the element must be self-closing like
<br/>. - Elements must nest properly: close the inner one before the outer one.
Attributes have their own rules: values must always be quoted (single or double quotes), and the same attribute cannot appear twice on one element.
<!-- Wrong -->
<item id=5 id="6"><b>Text</item></b>
<!-- Right -->
<item id="5"><b>Text</b></item>
Element Content Types
| Type | Example |
|---|---|
| Text only | <city>Pune</city> |
| Child elements only | <address><city>Pune</city></address> |
| Mixed content | <p>Read the <em>notes</em> first</p> |
| Empty | <verified/> or <photo src="a.png"/> |
Special characters inside text or attribute values must be escaped: write & for an ampersand and < for a less-than sign. For a long block containing many symbols, wrap it in a CDATA section.
Redesigning a Messy Document
Here is a record where too much is packed into attributes:
<order id="A17" items="Pen,Notebook" address="12 MG Road, Pune" note="Call before delivery"/>
The item list cannot hold quantities, the address cannot be split into parts, and the note may later need formatting. A cleaner version keeps the identifier as an attribute and moves data into elements:
<order id="A17">
<items>
<item qty="2">Pen</item>
<item qty="1">Notebook</item>
</items>
<address>
<street>12 MG Road</street>
<city>Pune</city>
</address>
<note>Call before delivery</note>
</order>
Notice that qty stays as an attribute: it is simple metadata describing each item.
Quick Design Checklist
- Is the value a list or likely to gain structure? Use elements.
- Is it an ID, unit, language or type flag? An attribute is fine.
- Does order matter? Element order is preserved; attribute order is not guaranteed.
- Will a schema validate it? Keep naming consistent so XSD rules stay simple.
Checking Your Document Is Well-Formed
Before sharing an XML file, open it in a browser or run it through a parser. Most browsers display a clear error with a line number if a tag is unclosed or an attribute is missing quotes. On the command line, xmllint --noout file.xml prints nothing when the document is well-formed and an error message otherwise. Being well-formed is the first step; validating against a schema later checks that the right elements and attributes appear in the right places.