XPath Nodes

There are seven kinds of nodes in XPath:

  1. Element
  2. Attribute
  3. Text
  4. Namespace
  5. Processing-instruction
  6. Comment
  7. Document nodes.

An XML document can be specified as a tree of nodes. The topmost element of the tree is called the root element.

Xpath Nodes 1

Let's take an example of XML document to understand the different terminology of XPath nodes.

An XML document:

snippet
<?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:

snippet
<library> (root element node)
<author>Chetan Bhagat</author> (element node)
lang="en" (attribute node)

Atomic values

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"

Relationship of Nodes

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.

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

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

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

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

snippet
<Library>
  <book>
    <title lang="en">Three Mistakes of My Life</title>
    <author>Chetan Bhagat</author>
    <year>2008</year>
    <price>110</price>
  </book>
</Library>
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents