According to the W3C, XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer. In this article we will learn about XPath, XPath expressions and how to use XPath in .NET and Java.
XPath, XML Path Language, is an expression language for addressing portions of an XML document or navigating within an XML document. We can also get values of elements and attributes using XPath. XPath is really helpful for parsing XML- based configuration or properties files. XPath is a major element in the XSLT standard.
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like URL and traditional file system paths. XPath also supports several functions for string manipulation, comparison and others.
First we will learn about these path expressions. XML documents are treated as trees of nodes and the root of the tree is called the document node or root node. There are about seven different kinds of nodes. They are element, attribute, text, namespace, processing-instruction, comment, and root nodes. Usually the most used nodes are element, attribute and text. Here is an XML sample.
In this XML sample, the root node is <bookstore>. We have two <book> elements; these are siblings. The <title> node is a child of the <book> element. Similarly, the <book> node is a parent of <title>, <author>, <year> and <price>. The <book> and <bookstore> nodes are ancestors of the <price> node. Similarly, the <book>, <title>, <author>, <year> and <price> nodes are descendants of <bookstore>, the root node. The id is an attribute node. The year value 2007 is a text node.