XPath Expression
XPath defines a pattern or path expression for selecting nodes or sets of nodes in an XML document.
These patterns are used by XSLT to perform transformations.
XPath Specifications
Nodes
XPath views an XML document as a tree of nodes. This tree starts at a single "root" and branches out to elements, their attributes, and their text content.
Consider this simple XML document:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J. K. Rowling</author>
</book>
</bookstore>
The XPath specification defines seven types of nodes that make up this tree structure:
- Root: The very top-level node of the document tree (conceptually, it's the parent of the root element, like
<bookstore>). - Element: The most common node type; it represents an XML element (e.g.,
<book>,<title>). - Attribute: Represents an attribute of an element (e.g.,
category="cooking"). - Text: Represents the text content inside an element (e.g., "Everyday Italian").
- Namespace: Represents a namespace declaration.
- Processing Instruction: Represents a processing instruction (e.g.,
<?xml-stylesheet ... ?>). - Comment: Represents a comment in the XML (e.g.,
<!-- My Comment -->).