Concepts / XML Document Structure and the Root Element

XML Document Structure and the Root Element

An XML element is a complete unit consisting of an opening tag, content, and a closing tag; it can also be represented as a self-closing tag if empty.

  • 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.

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

An element containing text

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

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

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

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

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

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.

attached todescribesphoneelementtype="intl"metadata<phone>opening tag
Where does an attribute attach, and how is its metadata associated with the element?

Separating content from metadata

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

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

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

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

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

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.

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

Tracing the source hierarchy

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

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

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

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

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

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.

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

Choosing an empty-element form

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

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

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

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

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

Mistakes Beginners Make

  • Treating an attribute as the element's primary content

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

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

  • Missing the parent-child relationship

    Nesting communicates which data belongs together.

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

  • Confusing an empty element with an invalid element

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

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

  • Ignoring the closing tag's matching name

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

    Fix: 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. A standard XML element consists of an opening tag, content, and a matching closing tag.
  2. Attributes are optional name="value" metadata attached to an opening tag.
  3. Nested elements create parent-child relationships and form a hierarchy under one root element.
  4. An empty element can use paired tags or a self-closing tag, and either form can include attributes.
  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.