XML XPath Nodes


XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath uses path expressions to select nodes or node-sets in an XML document. XPath includes over 200 built-in functions. There are functions for string values, numeric values, booleans, date and time comparison, node manipulation, sequence manipulation, and much more. Today XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

In the above XML document.

<bookstore> (root element node)
<author>J K. Rowling</author> (element node)
lang="en" (attribute node)
J K. Rowling (these atomic values have no parent or child)
"en" (these atomic values have no parent or child)

The book element is the parent of the title, author, year, and price. Element nodes may have zero, one or more children. In the example; the title, author, year, and price elements are all children of the book element. Siblings are nodes that have the same parent. Title, author, year, and price elements are all siblings. The ancestors of the title element are the book element and the bookstore element. Descendants of the bookstore element are the book, title, author, year, and price elements. Items are atomic values or nodes.

Leave a comment

Your email address will not be published. Required fields are marked *