XML Family Tree
You may have seen a lot of XML material make references to descendants, children, parents, or even great grandchildren and wondered to yourself, "Am I reading a family tree or am I learning XML?" The terminology that XML uses to describe the various relationships in a document can be confusing at first, which is why we wrote a set of lessons to clearly explain and show the most commonly used relation terms.XML - The Tree Structure
The great benefit about XML is that the document itself describes the structure of data. If any of you have researched your family history, you have probably come across a family tree. At the top of the tree is some early ancestor and at the bottom of the tree are the latest children.With a tree structure you can see which children belong to which parents, which grandchildren belong to which grandparents and many other relationships.
The neat thing about XML is that it also fits nicely into this tree structure, often referred to as an XML Tree.
Before you even think about matching up these tree terms with their XML counterparts, it might be helpful to see a real XML document converted into a tree structure. The lemonade.xml document we will be using was created by a particularly enterprising kid to keep inventory of her lemonade stand.
XML Code, lemonade.xml:
<inventory> <drink> <lemonade> <price>$2.50</price> <amount>20</amount> </lemonade> <pop> <price>$1.50</price> <amount>10</amount> </pop> </drink> <snack> <chips> <price>$4.50</price> <amount>60</amount> </chips> </snack> </inventory>
XML Tree:
Construction of an XML tree isn't all that hard to do, as long as you follow some basic rules.
XML Tree Rules
We've written up a set of rules to use in figuring out if an element is a child or parent of another element. With these basic rules, you can figure out the relationship of just about any two elements in an XML document.- Descendants: If element A is contained by element B, then A is a descendant of B. In the lemonade.xml example, every element is a descendant of inventory because inventory is the root element.
- Ancestors: If an element B contains element A, then B is an ancestor of A. In the lemonade.xml example, drink is the ancestor of lemonade and pop.
0 comments:
Post a Comment