XML Elements
An element is everything from a start tag up to and including its end tag. It is the basic building block of every XML document.
<city>Lisbon</city>Here <city> is the start tag, Lisbon is the content, and </city> is the end tag. Together they make one city element.
What an element can contain
| Content | Example |
|---|---|
| Text | <title>Moby-Dick</title> |
| Other elements | <author><first>Herman</first><last>Melville</last></author> |
| A mix of text and elements | <p>Call me <name>Ishmael</name>.</p> |
| Nothing (empty) | <ebook/> or <ebook></ebook> |
| Attributes (on the start tag) | <book isbn="978-0142437247">…</book> |
<book isbn="978-0142437247">
<title>Moby-Dick</title>
<author>
<first>Herman</first>
<last>Melville</last>
</author>
<opening>Call me <name>Ishmael</name>.</opening>
<ebook/>
</book>Empty elements
<ebook/> and <ebook></ebook> are exactly the same to an XML parser. Empty elements are useful as flags ("this book is also an e-book") or to carry only attributes, like <image src="cover.jpg"/>.
Naming rules
Element names must follow these rules:
- They may contain letters, digits, hyphens
-, underscores_and periods.. - They must start with a letter or underscore — not a digit, hyphen or period.
- They can't contain spaces.
- They should not start with the letters
xmlin any case (xml,XML,Xml…) — those names are reserved. - A colon
:is technically allowed but is reserved for namespace prefixes.
Try the broken names below one at a time to see which the parser rejects:
<inventory>
<item_count>12</item_count>
<last.updated>2026-01-31</last.updated>
<unit-price>4.99</unit-price>
<2ndWarehouse>Leeds</2ndWarehouse>
</inventory><2ndWarehouse> starts with a digit, so the document is not well-formed. Rename it to <secondWarehouse> or <warehouse2> and run again.
Naming good practice
The rules decide what is legal; good naming decides what is pleasant to use:
| Do | Avoid | Why |
|---|---|---|
<firstName> or <first_name> |
<the_first_name_of_the_person> |
Short and descriptive wins |
<price> |
<price-in-euro> when a currency attribute will do |
Keep units in data, not names |
<book> with a <title> child |
<booktitle> |
Let the tree express relationships |
| One consistent style | Mixing firstName and last_name |
Consistency avoids typos |
last_name |
last.name |
Some languages treat . as a property accessor; - can look like subtraction |
Tip: Non-English letters are allowed in names (
<größe>,<名前>), but pick plain ASCII if your files will be processed by older tools.
Elements are extensible
Suppose a program reads <book> elements and prints the title and author. If a later version of the data adds <publisher> and <pages>, the program keeps working, because it only looks for the elements it knows. This is a big advantage over positional formats like CSV, where adding a column can shift every field.
<books>
<book>
<title>Frankenstein</title>
<author>Mary Shelley</author>
</book>
<book>
<title>Dracula</title>
<author>Bram Stoker</author>
<publisher>Archibald Constable</publisher>
<pages>418</pages>
</book>
</books>Test yourself
Which element name is valid?
<1st-place><first place><first_place><-first>
Are <note/> and <note></note> different?
- Yes — one is empty, one contains a space
- Yes — only the first is allowed
- No — both are the same empty element
- No — but only in HTML