About Me

Friday, 13 April 2012

XML Tree - Descendant

XML Tree - Descendant

Any children that you have are your descendants. Any children that your children have are also your descendants. It doesn't matter how far down the family tree these children are; they will always be your descendants.

A descendant relationship is similar in XML. Element A is the descendant of element B if the following is true:
  • Element A is contained within element B.

Is an Element a Descendant?

If you are determining if one element is the descendant of another element, you will need to find the opening and closing tags of both elements to determine the relationship.
We have highlighted inventory and lemonade in our lemonade.xml document; try to determine determine which element is the descendant.

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>
lemonade is the descendant of inventory. This situation was a little tricky because lemonade is contained within another element, drink. However, it doesn't matter how far down the XML Tree an element is. If it's contained by another element, then it is that element's descendant.
Here is a graphical representation of the relationship between inventory and lemonade. You can clearly see that lemonade is the descendant.

XML Tree:

Bonus Question: What are the descendants of lemonade?
Answer: price and amount.

0 comments:

Post a Comment