Beginning XML Databases (Wrox Beginning Guides)
| ||
| ||
|
XPath recognizes numerous different types of nodes in XML documents. These nodes include element nodes, attribute nodes, text value nodes, namespace nodes, processing instruction nodes, comment nodes, and document nodes.
A document node is the equivalent of a root node, and an XML document is a tree of nodes containing a single root node.
Figure 10-1 shows some different node types, as defined by XPath.
In Figure 10-1, <?xml version="1.0"?> is a processing instruction node, < demographics > is the document or root node, <name>Africa</ name > is a text value node, and code=AG is an attribute node.
XPath Node Relationships
XPath node relationships simply define precise and obvious relationships between different nodes within an XML document. An atomic or text value has no relationship with any other nodes, in that it is a value of a text or attribute node. Atomic values are actual data values and not metadata. For example, in Figure 10-1 the following are atomic values:
Africa "AG"
A parent node is defined as being the parent of any element or attribute node. In the following example, the year node is the parent of all the nodes births_per_1000, deaths_per_1000, migrants_per_1000, natural_increase_percent, and growth_rate:
<year year="1950" population_id="12009" population="8892718"> <births_per_1000>0</births_per_1000> <deaths_per_1000>0</deaths_per_1000> <migrants_per_1000>0</migrants_per_1000> <natural_increase_percent>0</natural_increase_percent> <growth_rate>0</growth_rate> </year>
And similarly, in the preceding example, the nodes births_per_1000, deaths_per_1000, migrants_per_1000, natural_increase_percent, and growth_rate are all children of the year node.
Sibling nodes are nodes with the same parent and are on the same level of the node hierarchy. So once again, in the previous example, the nodes births_per_1000, deaths_per_1000, migrants_per_1000, natural_increase_percent, and growth_rate are all sibling nodes of one another.
Ancestor nodes are any nodes higher in the hierarchy of the tree, including parent node, parents parent (grandfather), and so on up the tree all the way up to the root node of the XML document. So once again, if you use the XML document from Figure 10-1, then the population, country, region, and demographics nodes are all ancestor nodes of the year node, as shown in the following example:
<demographics> <region id="1"> <country id="1" code="AG"> <population> <year year="1950" population_id="12009" population="8892718"> <births_per_1000>0</births_per_1000> <deaths_per_1000>0</deaths_per_1000> <migrants_per_1000>0</migrants_per_1000> <natural_increase_percent>0</natural_increase_percent> <growth_rate>0</growth_rate> </year>
Obviously, the descendants of a node are all its children, childrens children (grandchildren), and so on. So in the previous example, the nodes region, country, population, year, births_per_1000, deaths_per_1000, migrants_per_1000, natural_increase_percent, and growth_rate are all descendants of the document node.
| ||
| ||
|