XML Tree - Ancestor
Anyone that came before you in your family tree is your ancestor. This means your mom, dad, grandfather and great-great-great-grandmother are all your ancestors.
Although it may seem strange to some, XML has adopted this terminology to define relationships between various XML elements in the same "family". Element A is the ancestor of element B if the following is true:
- Element B is contained within element A.
If you are trying to eyeball if one element is an ancestor of another element, then you are going to need to find the opening and closing tags of both elements to determine the relationship.
We have highlighted pop and amount in our lemonade.xml document; you must determine which element is the ancestor.
XML Code, lemonade.xml:
If you said that pop was the ancestor of amount because pop contains amount, then you're right! Here's a graphical representation of the relationship. If you think of it just like a family tree, it becomes a lot easier!
What are the other elements that are the ancestor of the highlighted amount? If you said drink and inventory, you're right! That's because drink contains amount and inventory also contains amount.
Tip: The root element of an XML document is the ancestor of every element in the document. This is because all the elements are contained by the root element, by definition of a well-form XML document.
Download Tizag.com's XML Book
If you would rather download the PDF of this tutorial, check out our
XML eBook from the Tizag.com store.
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!