Concepts / Transforming XML with XSLT

Transforming XML with XSLT

XML documents are organized as tree structures with a single root element containing all other elements as nested children.

  • Programming

From Tags to a Tree

A deeply nested XML document can initially look like a jumble of angle brackets and text. The key to making it understandable is to stop viewing it only as a sequence of tags and instead view it as a tree. XML has a single root element, and every other element is organized beneath that root as a nested child. This hierarchical view is important when working with XML transformations because a transformation navigates through parts of this structure.

The root element is the top element in the XML tree. All other elements are descendants of that root.

containscontainscontainscontainspersonnamephonephoneemail
What contains what, and how are the XML elements organized beneath the single root element?

Reading Parent and Child Links

XML creates parent-child relationships through nesting. If one element contains another element, the containing element is the parent and the nested element is its child. Elements at the same level that share a parent are siblings. In the person example, person is the parent of name, both phone elements, and email. The two phone elements are siblings because they are at the same level and share person as their parent.

Each node represents an element. A node can hold the element's tag name, attributes attached to its opening tag, text content between its opening and closing tags, and child elements. This means that a tree view shows the structural relationships between elements, while the node itself can also carry properties and data.

containsparent-childparent-childparent-childparent-childcatalogrootbookparent of title, author,price, isbntitleauthorpriceisbn
Which element is the parent of a given element, and which elements are its direct children?

Classifying the Catalog Elements

Determine the root, the parent of price, and the siblings of title in the catalog tree.

Find the root: The root is the top element that contains the rest of the document. In this tree, that element is catalog.

Find price's parent: The element that directly contains price is book, so book is the parent of price.

Find title's siblings: Siblings share the same parent and occur at the same nesting level. The siblings of title are author, price, and isbn because all four elements share book as their parent.

The root is catalog. The parent of price is book. The siblings of title are author, price, and isbn.

Following a Traversal

XML trees can contain several nested levels. To locate a specific element, begin at the root and follow the chain of parent-child relationships downward. This movement through the tree is called a traversal. The path records the elements visited on the way to the target.

Finding Alice in a Company Tree

Use the company hierarchy to locate the member whose text is Alice.

Start at company: company is the root element, so the traversal begins there.

Move to a department: Each department is nested beneath company. Choose the first department when following the example path.

Move to a team: The selected department contains team elements. Follow the path to the first team.

Locate the member: The team contains member elements. Find the member element whose text is Alice.

The traversal path is company → department → team → member.

downdowndowncompanyrootdepartmentteammemberAlice
What path do you follow from the root through nested children to reach a specific piece of data?

Why the Tree View Matters

The tree is more than a drawing. It clarifies how XML data is organized hierarchically and helps you reason about operations that work with XML. Parsers read and process the document according to this structure. Query tools navigate through it, XPath describes paths through it, and XSLT transformations move from one part of the tree to another. A schema also defines an allowed tree structure for validation.

Before attempting to understand a query or transformation, sketch the XML as a tree. Mark the root, identify each element's direct children, and trace the path to the data you need. This turns a long sequence of nested tags into a set of visible relationships.

  • Treating XML as flat text instead of hierarchical data

    The important relationships in XML come from nesting, and those relationships determine the tree structure.

    Fix: Identify the root first, then arrange each nested element beneath its containing parent.

  • Calling elements at different levels siblings

    Siblings must share the same parent and be at the same nesting level.

    Fix: Check the direct parent of each element before deciding whether two elements are siblings.

  • Starting a traversal from a nested element

    A traversal through the document is described from the root down through parent elements to the target.

    Fix: Write the complete path: company → department → team → member.

Practice the Path

EASY

A tree has root element library. Beneath it is book. Beneath book are title, author, and price. Identify the root, the parent of author, and the siblings of price.

Hints
  • The root is the top element.
  • The parent is the element that directly contains the target.
  • Siblings share the same parent and nesting level.

What do you think happens?

Before checking the answer, predict the traversal path to a member nested inside the first team of the first department under company.

  • member → team → department → company
  • company → department → team → member
  • company → team → department → member
Reveal answer

Answer: company → department → team → member

The path starts at the root and follows each parent-child level until it reaches the member element.

Summary

  1. An XML document is organized as a tree with one root element and nested descendants.
  2. Nesting creates parent-child relationships; elements with the same parent at the same level are siblings.
  3. Each element node can have attributes, text content, and child elements.
  4. A traversal follows a path from the root through nested elements to locate specific data.
  5. Understanding the tree makes XML parsing, querying, validation, and XSLT transformation easier to reason about.

Key Takeaways

  • XML is best understood as a tree rather than as flat text.
  • The single root contains all other elements through nested parent-child relationships.
  • Sibling elements share a parent and occupy the same nesting level.
  • A traversal follows the path from the root through nested elements to a target.
  • The tree model provides the foundation for understanding XML queries and transformations with XSLT.