There are seven kinds of nodes in XPath:
An XML document can be specified as a tree of nodes. The topmost element of the tree is called the root element.
Let's take an example of XML document to understand the different terminology of XPath nodes.
An XML document:
<?xml version="1.0" encoding="UTF-8"?> <Library> <book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book> </Library>
Nodes in the above XML document:
<library> (root element node) <author>Chetan Bhagat</author> (element node) lang="en" (attribute node)
Atomic values are used to specify the nodes with no children or parent. For example: In the above XML document, following are the atomic values:
Chetan Bhagat
"en"
Parent Node
Each element and attribute has a parent which is a top element of the respective element or attribute.
See this example:
In this example, the book element is the parent of the title, author, year, and price.
<book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book>
Children Nodes
The children nodes can have zero, one or more children. In this example, the title, author, year, and price elements are all children of the book element.
<book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book>
Siblings Nodes
The nodes having the same parent are known as siblings. In this example, the title, author, year, and price elements are all siblings.
<book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book>
Ancestors
A node's parent or parent's parent is specified as ancestor. In this example, the ancestors of the title element are the book element and the library element.
<Library> <book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book> </Library>
Descendants
A descendent is specified as a node's children or children's children. In this example, descendants of the library element are the book, title, author, year, and price elements.
<Library> <book> <title lang="en">Three Mistakes of My Life</title> <author>Chetan Bhagat</author> <year>2008</year> <price>110</price> </book> </Library>