Benefits of XPath
XPath is designed for XML documents. It provides a single syntax that you can use for queries, addressing, and patterns. XPath is concise, simple, and powerful.
XPath has many benefits:
- Syntax is simple for the simple and common cases.
- Any path that can occur in an XML document and any set of conditions for the nodes in the path can be specified.
- Any node in an XML document can be uniquely identified.
XPath is designed to be used in many contexts. It is applicable to providing links to nodes, for searching repositories, and for many other applications.
XML Document in XPath
In XPath, an XML document a viewed conceptually as a tree in which each part of the document is represented as a node as shown in figure below:
XPath has seven types of nodes. They are:
- Root: The XPath tree has a single root node, which contains all other nodes in the tree.
- Element: Every element in a document has a corresponding element node that appears in the tree under the root node. Within an element node appear all of the other types of nodes that corresponding to the element’s content. Element nodes may have a unique identifier associated with them that is used to reference the node with XPath.
- Attribute: Each element node has an associated set of attribute nodes, the element is the parent of each of these attribute nodes, however, an attribute node is not a child of its parent element.
- Text: Character data is grouped into text nodes. Characters inside comments, processing instructions and attribute values do not produce text nodes. The text node has a parent node and it may the child node too.
- Comment: There is a comment node for every comment, except for any comment that occurs within the documents type declaration. The comment node has a parent node and it may be the child node too.
- Processing instruction: There is a processing instruction node for every processing instruction, except for any processing instruction that occurs within the document type declaration. The processing instruction node has a parent node and it may be the child node too.
- Namespace: Each element has an associated set of namespace nodes. Although the namespace node has a parent node, the namespace node is not considered a child of its parent node because they are not contained in a parent node, but are used to provide descriptive information about their parent node.