XML Attributes and Text Content
XML documents are organized as tree structures with a single root element containing all other elements as nested children.
From Tags to a Tree
A deeply nested XML document can look like a jumble of angle brackets and text when viewed as plain text. A clearer way to understand it is to represent the document as a tree. The tree shows which elements contain other elements, starting with one root element at the top and branching downward through nested children.
An XML document has a single root element. Every other element is contained somewhere below that root. Nesting creates parent-child relationships: an element that contains another element is its parent, and the contained element is its child. Elements at the same level with the same parent are siblings.
Reading Parent and Sibling Relationships
In the person tree, person is the root element and therefore has no parent within this document. name, both phone elements, and email are children of person. Because the two phone elements share the same parent and occupy the same level, they are siblings. The same relationship can repeat at deeper levels: a child can also be a parent if it contains further nested elements.
Nesting, not the visual order of the tags alone, creates the relationship. The containing element is the parent; the contained element is the child. Sibling elements share a parent and are at the same nesting level.
| Element | Relationship | Reason |
|---|---|---|
| person | Root | It is the top element in the document |
| phone | Child of person | It is nested inside person |
| The two phone elements | Siblings | They share person as their parent and are at the same level |
Relationships in the person tree
What an Element Can Hold
Each node in an XML tree represents an element. An element can hold three distinct kinds of information: the element itself, attributes attached to its opening tag, and text content placed between its opening and closing tags. An element can also contain child elements. Attributes and text content are properties of the element node, while child elements create additional branches in the tree.
Consider an illustrative element named person with an attribute id whose value is p01, and with Alice between its opening and closing tags. In this example, id and p01 represent attribute information attached to the opening tag. Alice represents text content. If the person element also contains name or phone elements, those are child elements rather than text content.
Following a Traversal Path
A tree becomes especially useful when the information is nested several levels deep. To locate a particular value, begin at the root and follow the chain of parent-child relationships downward. This movement through the tree is called a traversal.
Finding Alice in a company tree
Locate the member whose text content is Alice in a tree where company contains department elements, each department contains team elements, and each team contains member elements.
- 1Start at the root
Begin with company, the root element.
- 2Enter a department
Move from company to the relevant department child.
- 3Enter a team
Move from that department to the relevant team child.
- 4Inspect members
Look among the team element's member children for the member whose text content is Alice.
The traversal path is company → department → team → member. The target is the member element with text content Alice.
The path matters because it records the hierarchy, not just the names of the elements. A member under one team is reached through that team, its department, and company. XML parsers and query tools operate by navigating this kind of tree. XPath describes paths through the tree, XSLT moves from one part of the tree to another during transformation, and a schema defines an allowed tree structure.
Mistakes in Tree Reading
Common mistake Treating every element at the same visual indentation as a sibling
What it looks likeAssuming two elements are siblings without checking whether they share the same parent
Why it failsSibling status depends on having the same parent, not merely appearing near each other.
Do this insteadTrace each element upward and identify the element that contains it.
Common mistake Starting a traversal from a nested element
What it looks likeLooking for Alice by searching directly for member without first identifying its containing team and department
Why it failsA traversal is a path from the root through nested parent-child relationships.
Do this insteadBegin at the root and follow the path company → department → team → member.
Common mistake Calling an attribute text content
What it looks likeTreating an id value attached to an opening tag as the text between the element's tags
Why it failsAttributes are key-value pairs attached to an opening tag, while text content is between the opening and closing tags.
Do this insteadClassify the information by its position: opening-tag key-value pair means attribute; between tags means text content.
Common mistake Ignoring child elements because an element also has text content
What it looks likeAssuming an element can have text content but cannot also contain child elements
Why it failsA node can hold attributes, text content, and child elements.
Do this insteadInspect all three parts of the element when interpreting its tree representation.
Practice the Path
A catalog element contains a book element. The book element contains title, author, price, and isbn elements. Identify the root element, the parent of price, and the siblings of title.
Hints
- The root is the single top element.
- Find the element that directly contains price.
- Siblings share the same parent and occupy the same nesting level.
What do you think happens?
What are the root, parent, and sibling elements in the catalog example?
Reveal answer
The root element is catalog. The parent of price is book. The siblings of title are author, price, and isbn.
catalog is the top element. book directly contains price. title, author, price, and isbn share book as their parent and are therefore siblings.
Hierarchical XML Thinking
- 1An XML document is represented as a tree with one root element and nested children.
- 2Nesting creates parent-child relationships, while elements with the same parent are siblings.
- 3Each element node can hold attributes, text content, and child elements.
- 4To locate nested data, start at the root and follow a traversal path through parent elements.
- 5Tree visualization supports understanding of XML parsers, validators, queries, and transformations.
Key Takeaways
- XML's single root element contains all other elements in a tree-shaped hierarchy.
- Parent, child, and sibling relationships come from how elements are nested.
- Attributes belong to an opening tag, while text content appears between opening and closing tags.
- A traversal follows a path from the root through nested elements to reach specific data.
- Visualizing the tree makes XML structure easier to understand and navigate.