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:
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.
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.
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.
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
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