Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XPath Nodes

XPath Nodes


XPath terminology

Node

In XPath there are seven kinds of nodes: element, attribute, text, namespace, processing instruction, comment and document (root) node. An XML document is treated as a tree of nodes. The root of the tree is called the document node or root node.

Look at this XML document:

 

    Harry Potter

    J K. Rowling

    2005

    29.99

 

Examples of nodes in the XML document above:

(document node)

J K. Rowling (element node)

lang="en" (attribute node)

Atomic values

An atomic value is a node with no parent and no children.

Examples of atomic values:

J K. Rowling

"en"

Items

An item is an atomic value or a node.


Node relationships

Parent

Every element and attribute has a parent.

In the example below, the book element is the parent of the title, author, year and price elements:

  Harry Potter

  J K. Rowling

  2005

  29.99

Children

An element node may have zero, one or more children.

In the example below, the title, author, year and price elements are all children of the book element:

  Harry Potter

  J K. Rowling

  2005

  29.99

Siblings

Nodes that have the same parent

In the example below, the title, author, year and price elements are all siblings:

  Harry Potter

  J K. Rowling

  2005

  29.99

Ancestors

A node's parent, its parent's parent, and so on.

In the example below, the ancestors of the title element are the book element and the bookstore element:

  Harry Potter

  J K. Rowling

  2005

  29.99

Descendants

A node's children, its children's children, and so on.

In the example below, the descendants of bookstore are the book, title, author, year and price elements:

  Harry Potter

  J K. Rowling

  2005

  29.99