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:
|
0 1 2 3 4 5 6 |
<tagname>content here</tagname> |
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:
|
0 1 2 3 4 5 6 7 8 |
<name>Rahul Sharma</name> → box labeled "name" containing text <price>1499.00</price> → box labeled "price" <address>...</address> → box labeled "address" containing more boxes |
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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
<student> <rollNumber>101</rollNumber> <fullName>Priyanka Das</fullName> <class>XI</class> <percentage>94.6</percentage> <isActive>true</isActive> </student> |
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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<order> <orderId>ORD-20250625-6741</orderId> <customer> <name>Samarth Patel</name> <email>samarth.p@example.com</email> <phone>+919876543210</phone> </customer> <items> <item> <sku>TS-BLK-M</sku> <name>Black T-Shirt Medium</name> <quantity>2</quantity> <unitPrice>499.00</unitPrice> </item> <item> <sku>HDMI-CBL-2M</sku> <name>HDMI Cable 2m</name> <quantity>1</quantity> <unitPrice>349.00</unitPrice> </item> </items> <total>1347.00</total> </order> |
Here:
- <order>, <customer>, <items> are parent elements
- They exist mainly to organize / group other elements
C. Empty Elements (two correct syntaxes)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- Most common modern style --> <image src="product-front.jpg" alt="Black T-shirt" width="300"/> <flag country="IN"/> <isPaid/> <!-- Also 100% correct (older style) --> <image src="product-front.jpg" alt="Black T-shirt" width="300"></image> <br></br> |
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.
|
0 1 2 3 4 5 6 7 8 9 10 |
<description> This is a very comfortable <b>cotton t-shirt</b> available in <strong>black, navy blue and grey</strong>. Free shipping on orders above ₹999. </description> |
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:
|
0 1 2 3 4 5 6 |
<product id="P-784512" category="clothing" inStock="true" discount="10"> |
| 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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="UTF-8"?> <invoice> <header> <invoiceNumber>INV/2025/0892</invoiceNumber> <issueDate>2025-06-25</issueDate> <dueDate>2025-07-10</dueDate> </header> <seller> <name>TechTrend Innovations</name> <gstin>36AAECT4567P1Z2</gstin> <address> <street>Plot 45, Hi-Tech City</street> <city>Hyderabad</city> <state>Telangana</state> <pincode>500081</pincode> </address> </seller> <items> <item line="1"> <description>Annual LMS License</description> <quantity>1</quantity> <rate>185000.00</rate> <amount>185000.00</amount> </item> <item line="2"> <description>Custom Training (8 hours)</description> <quantity>1</quantity> <rate>45000.00</rate> <amount>45000.00</amount> </item> </items> <totals> <subtotal>230000.00</subtotal> <gst>41400.00</gst> <grandTotal currency="INR">271400.00</grandTotal> </totals> </invoice> |
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! 😊
