Chapter 6: XML Elements

1. What is an XML Element? (The simplest honest definition)

An XML element is the basic building block of XML.

It consists of:

  • An opening tag (sometimes called start tag)
  • Content (which can be text, other elements, or nothing)
  • A closing tag (sometimes called end tag)

The most common visual form:

XML

Real-life analogy:

Think of an XML element as a labeled box:

  • The tag name is the label written on the box
  • The content is whatever you put inside the box
  • The opening and closing tags are like the lid being put on and taken off

Examples:

XML

2. Four Main Kinds of Elements You Will See

Type Looks like this Contains what? Common name(s)
Element with text <title>Atomic Habits</title> Only text Text-only element, leaf element
Element with children <book><title>…</title></book> Other elements (no direct text) Parent element, container element
Empty element <image src=”photo.jpg”/> or <br></br> Nothing Empty element, void element, self-closing
Mixed content <p>Hello <b>world</b>!</p> Text + child elements mixed Mixed-content element

Let’s look at each one in detail.

A. Element with Text Content (most common for data)

XML

These are often called leaf elements or terminal elements because they have no children — only text.

Important note: Everything inside is text — even numbers and true/false are stored as text characters.

B. Element with Child Elements (containers / parents)

XML

Here:

  • <order>, <customer>, <items> are parent elements
  • They exist mainly to organize / group other elements

C. Empty Elements (two correct syntaxes)

XML

Rule: If there is no content (no text and no child elements), you can write it either way.

Most people in 2025–2026 prefer the short form: <tag/>

D. Mixed Content (text + elements together)

This is more common in documents than in data.

XML

Here <description> contains:

  • normal text
  • child elements <b> and <strong>
  • more normal text

This style is very common in:

  • Word processing (like .docx internal format)
  • Publishing (DocBook, DITA)
  • XHTML / HTML-like content inside XML

3. Anatomy of an Element – Every Part Explained

Take this line:

XML
Part Name Explanation
< Opening angle bracket Marks the start of a tag
product Element name / tag name The actual name of the element (case-sensitive!)
id=”P-784512″ Attribute Extra information about this element
category=”clothing” Another attribute You can have many attributes (all must have unique names)
> Closing angle bracket Ends the opening tag
… content goes here … Content Can be text, child elements, both, or nothing
</product> Closing tag Must match the opening tag name exactly (same case!)

4. Naming Rules for Elements (Very Important)

Allowed Not allowed Reason / Problem
<product> <product name> Space not allowed
<ProductDetails> <Product-Details> – usually not allowed in many parsers
<user_id> <user_id#> # is not allowed
<Book> <book> and </Book> Case must match exactly
<price-inr> <2ndItem> Cannot start with number
<description> <xml-book> Cannot start with xml (reserved)

Best practice naming styles (2025–2026):

  • CamelCase (most common in data): <ProductName>, <OrderStatus>
  • snake_case: <product_name>, <order_status>
  • kebab-case: <product-name> (less common, but valid)

Choose one style and be consistent across the whole document.

5. Very Realistic Example – Complete Structure

XML

This shows:

  • Many parent elements (<invoice>, <header>, <seller>, <items>, <totals>)
  • Many text-only elements (<invoiceNumber>, <quantity>, <grandTotal>)
  • Nested hierarchy (address inside seller)
  • Attributes used for small metadata (line=”1″, currency=”INR”)

Quick Summary – What You Should Remember About Elements

  • Element = opening tag + content + closing tag
  • There are text-only, parent, empty, and mixed-content elements
  • Tag names are case-sensitive and follow strict naming rules
  • Empty elements can be written <tag/> or <tag></tag>
  • Attributes belong to the element (they are not children)
  • Everything is nested — proper nesting is mandatory

Where would you like to go next?

  • Difference between elements vs attributes (with decision guidelines)
  • Naming conventions people really use in serious projects
  • Elements with namespaces (why they exist and how they look)
  • Mixed content in detail (with real examples from documents)
  • Common mistakes people make with elements
  • How elements appear in real formats (SOAP, Android manifest, e-invoice, Office XML…)

Just tell me what feels most useful or interesting right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *