Aral Code

Structure Lesson 3 of 9

XML Elements, Attributes and Tree Structure

Understand the XML tree of parent, child and sibling elements, choose between elements and attributes, design clear XML for records like a class list, and use namespaces.

An XML document is a tree. The root element is the trunk, and every element inside it is a branch. Designing good XML means choosing clear names and a sensible structure.

The XML tree

<school name="Rizal NHS">
  <section grade="10" name="Rizal">
    <student lrn="136728140001">
      <name>Ana Reyes</name>
      <average>91.5</average>
    </student>
    <student lrn="136728140002">
      <name>Ben Santos</name>
      <average>84.0</average>
    </student>
  </section>
</school>
school
└── section
    ├── student
    │   ├── name
    │   └── average
    └── student
        ├── name
        └── average
  • school is the root.
  • section is the parent of the two student elements; they are its children and each other's siblings.
  • Text like "Ana Reyes" is the element's content.
  • lrn="..." is an attribute.

Elements vs attributes

The same data can be written two ways:

<!-- as attributes -->
<student lrn="136728140001" name="Ana Reyes" average="91.5"/>

<!-- as child elements -->
<student>
  <lrn>136728140001</lrn>
  <name>Ana Reyes</name>
  <average>91.5</average>
</student>
Use an attribute for…Use an element for…
Identifiers and metadata: id, lrn, lang, unitThe actual data
Short, simple valuesValues that might have structure later (an address with street and city)
One value per nameRepeated values (several phone numbers)

A common guideline: data goes in elements, information about the data goes in attributes. For example, <price currency="PHP">25.00</price>.

Repeating elements

<student lrn="136728140001">
  <name>Ana Reyes</name>
  <phones>
    <phone type="mobile">09171234567</phone>
    <phone type="home">0281234567</phone>
  </phones>
</student>

Attributes can't repeat on the same element, so lists always use child elements, often wrapped in a plural container like <phones>.

Good naming

  • Descriptive: <birthDate> not <bd>.
  • Consistent style: pick camelCase (firstName) or hyphens (first-name) or underscores, and stick with it.
  • Avoid dots and colons in names (colons are reserved for namespaces).

Namespaces

When XML from different sources is combined, tag names can clash: a <table> of furniture vs an HTML <table>. Namespaces give names a unique prefix tied to a URI:

<root xmlns:h="http://www.w3.org/1999/xhtml"
      xmlns:f="https://example.com/furniture">
  <h:table><h:tr><h:td>Grades</h:td></h:tr></h:table>
  <f:table><f:material>Narra wood</f:material></f:table>
</root>

The URI is just a unique label; it doesn't need to be a real web page. You'll see namespaces in sitemaps (xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"), SVG and Office files. A plain xmlns="..." without a prefix sets the default namespace for the element and its children.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. In the example, what is the relationship between two student elements in the same section?
2. What's a common guideline for attributes vs elements?
3. Why use child elements for several phone numbers?
4. What problem do namespaces solve?

By Sir Reyel · Last updated 2026-12-04