Concepts / XML Document Structure and the Root Element

XML Document Structure and the Root Element

The element name in the opening and closing tags must match. The closing tag is not a second, unrelated label; it marks where the same element ends.

  • Programming

Reading XML as Organized Data

XML is designed to organize data so that both humans and machines can understand it. When you read XML in a configuration file, an API response, or a data export, you are working with combinations of elements, attributes, and nesting. These parts create a strict and predictable structure: elements hold data, attributes describe elements, and nesting shows relationships between data.

The Three Parts of an Element

An XML element is a complete unit consisting of an opening tag, content, and a closing tag. The opening tag begins with a less-than symbol, contains the element name, and ends with a greater-than symbol. The closing tag uses the same element name but places a forward slash before that name. Everything between the two tags is the element's content.

<name>opening tagChuckcontent</name>closing tagcontainsends at
Which parts make up a complete XML element, and where does the content appear?
Worked example

An element containing text

Identify the three parts of the name element in the source example.

  1. 1
    Opening tag

    The name element begins with the opening tag <name>.

  2. 2
    Content

    The text Chuck appears between the tags, so it is the element's content.

  3. 3
    Closing tag

    The element ends with </name>, which matches the opening element name and includes a forward slash.

Result

The name element is a complete unit made from <name>, the content Chuck, and </name>.

Key point

The element name in the opening and closing tags must match. The closing tag is not a second, unrelated label; it marks where the same element ends.

Metadata in Opening Tags

An attribute is optional metadata attached to an element's opening tag. Its form is name="value": an attribute name, an equals sign, and a quoted value. Multiple attributes can appear on one tag, separated by spaces. Attributes describe or modify the element, but an element does not need to have any attributes.

phoneelementtype="intl"metadata<phone>opening tagattached todescribes
Where does an attribute attach, and how is its metadata associated with the element?
Worked example

Separating content from metadata

In the phone example, decide which information is the primary data and which information describes that data.

  1. 1
    Element content

    The phone number appears between the phone opening and closing tags. It is the primary content of the phone element.

  2. 2
    Attribute

    type="intl" appears in the opening tag. It identifies what kind of phone number the content represents.

  3. 3
    Design choice

    The phone number belongs as content, while its international classification belongs as metadata in an attribute.

Result

Use attributes for information that describes an element, and use element content for the primary data.

Best practice

Use attributes for metadata, properties, or classifications such as type, id, status, or version. Use nested elements for primary content or for data that may have its own structure or attributes.

Parent, Child, and Root

XML elements can be nested inside other elements, creating a tree-like hierarchy. An element that contains other elements is a parent. The elements inside it are children. The outermost element that contains all the others is the root element. A document's single root element provides the top-level container for the rest of the XML structure.

personrootnameChuckphonetype="intl"emailempty elementcontainscontainscontains
What contains what, and how do nested elements connect to the single root element?
Worked example

Tracing the source hierarchy

Use the source person example to identify the root, its children, and the content of those children.

  1. 1
    Find the outermost element

    person contains all the other elements, so person is the root element.

  2. 2
    Find the children

    name, phone, and email are directly inside person, so they are person’s three children.

  3. 3
    Inspect each child

    name contains the text Chuck. phone contains text and has the attribute type="intl". email has no content.

Result

The hierarchy records that name, phone, and email all belong to person.

Key point

Nesting communicates ownership and relationships. Because phone and email are inside person, the structure shows that they belong to that person rather than to some other entity.

Representing Empty Elements

An element with no text and no child elements is empty. XML provides two valid ways to represent it. The traditional form uses an opening tag followed immediately by a closing tag. The concise form uses a self-closing tag, which combines the opening and closing into one tag ending with a forward slash before the closing bracket.

<email></email>opening and closing tags<email hide="yes" />one self-closing tagsame meaning
What changes in the tag structure when an element has no content?
Worked example

Choosing an empty-element form

Compare the two valid representations of an email element that has no content.

  1. 1
    Paired form

    An opening email tag followed by a closing email tag represents an empty element because nothing appears between the tags.

  2. 2
    Self-closing form

    The self-closing form combines both tags and places a forward slash before the closing bracket.

  3. 3
    Attribute check

    An empty element can still carry metadata, as shown by hide="yes" in the self-closing form.

Result

The paired and self-closing forms are both valid and mean the same thing; the self-closing form is more compact.

Mistakes Beginners Make

  • Common mistake

    Treating an attribute as the element's primary content

    What it looks like

    Putting the phone number's main data into the classification attribute instead of keeping the number as element content

    Why it fails

    Attributes describe or classify an element, while nested content stores the primary data.

    Do this instead

    Keep the phone number between the phone tags and use type="intl" only for the classification.

  • Common mistake

    Missing the parent-child relationship

    What it looks like

    Reading name, phone, and email as unrelated items instead of children of person

    Why it fails

    Nesting communicates which data belongs together.

    Do this instead

    Look for the outermost element and identify the elements directly contained inside it.

  • Common mistake

    Confusing an empty element with an invalid element

    What it looks like

    Assuming that email is invalid because it has no text

    Why it fails

    An element with no text or child elements is still valid when represented with paired tags or a self-closing tag.

    Do this instead

    Recognize both the paired empty form and the self-closing form as valid representations.

  • Common mistake

    Ignoring the closing tag's matching name

    What it looks like

    Treating a closing tag as unrelated to its opening tag

    Why it fails

    A standard XML element is completed by a matching opening and closing tag.

    Do this instead

    Compare the element names and check that the closing tag includes the forward slash before the name.

Check Your Understanding

EASY

Using the source person structure, identify the root element, list its three children, identify which child contains text, identify which child has an attribute, and identify which child is empty.

Hints
  • The root is the outermost element containing all the others.
  • The children are the elements directly inside person.
  • Check each child for text content, an attribute, or no content.

What do you think happens?

The email element has no text or child elements. Can it still include metadata?

  • Yes, an empty element can still have an attribute

  • No, attributes require text content

  • No, only the root element can have attributes

Reveal answer
Answer:

Yes, an empty element can still have an attribute.

The source gives <email hide="yes" /> as a valid self-closing tag with an attribute.

Structure to Remember

  1. 1
    A standard XML element consists of an opening tag, content, and a matching closing tag.
  2. 2
    Attributes are optional name="value" metadata attached to an opening tag.
  3. 3
    Nested elements create parent-child relationships and form a hierarchy under one root element.
  4. 4
    An empty element can use paired tags or a self-closing tag, and either form can include attributes.
  5. 5
    Use attributes for metadata and classifications; use nested elements for primary content or structured child data.

Key Takeaways

  • XML elements are complete units made from tags and content.
  • Attributes attach to opening tags and describe the element with quoted metadata values.
  • Nesting creates a tree-like structure of parents and children.
  • The root element is the single outermost element containing the document's other elements.
  • Empty elements may use paired tags or a self-closing tag.

Related concepts

More concepts starting with X