Node Relationships

Understanding how XML node relationships function is a prerequisite of document manipulation, addressing and and queries (such as XPATH). An XML document can define the following relationships between the various nodes. In each case the relationship is determined from the context of the current node.

The hypothetical bookstore example is used (once again) to assist in illustrating each of the various relationships between nodes:

<?xml version="1.0" encoding="UTF-8"?>
<library>
<category name="dogs">
<book>
<title lang="english">All about dogs</title>
<author>Someone</author>
<isin>true</isin>
<daysuntilreturn>0</daysuntilreturn>
<price>15.10</price>
</book>
</category>
<category name="cats">
<book>
<title lang="english">All about cats</title>
<author>Someone</author>
<isin>false</isin>
<daysuntilreturn>3</daysuntilreturn>
<price>12.00</price>
</book>
<book>
<title lang="zulu">Konke mayelana ikati</title>
<author>Else</author>
<isin>true</isin>
<daysuntilreturn>0</daysuntilreturn>
<price>25.99</price>
</book>
</category>
</library>

Parent Relationship

The parent node is always one level higher than the current node with a direct relationship between the current node and the parent node. A node can only have one parent node; however, a parent node can have zero or more child nodes.

Parent Relationship

Child Relationship

Child nodes are always one level lower than the current node with a direct relationship between the current node and any child nodes. A node can have zero or more children.

Child Relationship

Sibling Relationship

Sibling nodes are always on the same level of the document hierarchy. Each sibling shares the same parent node, in other words, a parent has children which are siblings. A node can have zero or more siblings.

Sibling Relationship

Ancestor Relationship

Ancestor nodes are further up in the document hierarchy. A node is an ancestor if there is a direct path from the current node up through the relationships e.g.. parent, grandparent, great-grand parent and etc.

  • A parent node is an ancestor of the current node.
  • The documents’ root node is the highest level node and is an ancestor of every other node in the document.
  • The root node itself cannot have an ancestor or parent

Ancestor Relationship

Descendant Relationship

Descendant nodes are further down in the document hierarchy from the current node. There must be a direct path from the current node to the node in question via the relationships.

  • A child node is a descendent
  • The child of a child, of a child…, of a child is a descendent of the current node.
  • Every node in the document is a descendent of the root node

Descendant Relationship

Posted in XML, XPATH and tagged .

Leave a Reply