Updated April 13, 2023
Introduction to XPath Nodes
Xpath Node is defined as a point where the path address initiates, as it follows a concept of nodes. In simple terms they are the individual elements of the Xpath hierarchical structure which are termed as a node and enable an XSL processing. Xpath expressions could be done with HTML and XML. It defines a way to assign a string-value for each type of node in the document as they have a standard library function to work with strings and other Boolean expressions. Ordering sequencing the nodes is done with the document order and Xml nesting elements as well. It basically selects any kind of nodes in the tree structure.
Types of XPath Nodes
XPath includes seven different types of nodes with different functions they are listed below which are self-explanatory. These are the various node Kinds.
- Element: These type follows the root node and can contain attribute in it. And the node tests are node()
- Attribute: This can be assigned below the element or root node and denoted using ‘@’. attribute:: * which matches all the nodes.
- namespace: NCName: * matches namespace for all the nodes.
- text: Represents the text written inside an element node and represented as text ().
- comment: This node specifies the comment part for the particular code which is not processed by any programming language compiler and assigned as a comment ().
- Processing instruction: this specifies the instruction for the application and can be used anywhere in the XML document. And declared as Processing-instruction ().
- Root Nodes: This is the topmost element of the document with the child elements under it.
Path to Our Element
The path to our element is given below:
<?xml version="1.0"?> [ XML Declaration]
<?example demo?>
<!DOCTYPE Delegate SYSTEM "Delegate.dtd"> [Root Node – People]
<Delegate xmlns="http://www.delegate.org/NS/Delegate567"> [ Element Node with Namespace node]
<! -- List of employees--> [Comment Node]
<employee empID="5425"> [ Attribute node empid]
details: <EName>Joy Evangeline</EName>
Designation: <Position>Senior Manager</Position>
</employee>
<employee StaffID="98647">
details: <EName>Bill Black</EName>
Designation: <Position>Web Programmer</Position>
</employee>
</Delegate>
Examples to Implement XPath Nodes
Let’s see an example in identifying the nodes and relationships between them in a XML Document.
order.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<!--Ticket is non-Endorsable!-->
<travel date="2018-03-04">
<details xmlns:airline="http://localhost/XML/delivery" xmlns:bookinginfo="http://localhost/XML/bookinginfo">
<airline:name>Qatar Airways</airline:name>
<airline:Operatingnumber>Boeing 453</airline:Operatingnumber>
<airline:type>Economy</airline:type>
<airline:time>20.30</airline:time>
<airline:Departure>Doha</airline:Departure>
<airline:country>dallas</airline:country>
<bookinginfo:Passengername>Saelvan Clavin</bookinginfo:Passengername>
<bookinginfo:city>Hyderabad</bookinginfo:city>
<bookinginfo:state>Telengana</bookinginfo:state>
<bookinginfo:country>India</bookinginfo:country>
</details>
<comment>Please use Hand Sanitizer! , Have a pleasant flight</comment>
<Purchase>
<cd cdno="H5284756">
<title>Narnia- The Legend</title>
<qty>5</qty>
<price>25.84</price>
<comment>Please confirm the status</comment>
</cd>
<cd cdno="M635854">
<title>Be-hind Enemy Lines</title>
<qty>1</qty>
<price>17.74</price>
</cd>
</Purchase>
</travel>
The above code is explained with the different types individually
1. Element Node
This node starts at the beginning and ends with an end tag. The first element node in the XML tree sequence is called as root elements. The XML document shown above has the element node travel to be considered as a root element. This is treated as a parent node for the following element nodes billing info, comment, purchase which again consists of additional node as child nodes(elements).
2. Attribute Nodes
The Xpath data model represents attributes of an XML element in the document as attribute nodes provided with an identifier and some value or string assigned to that specific attribute. This attribute node usage in Xpath is completely different than in DOM in the part of the element node as a parent. They are considered to be in the element node not as a child element. In the above XML code, the first element node contains cd and the attribute node as “cdno” with the respective value “H5284756”.
<cd cdno="M635854">
3. Text Node
The character text marked with the starting and end of the tags of a particular element node as considered to be a Text node. In the above XML code, the element node like “title” has the quoted text as Narnia and behind an enemy, lies are referred to as text node.
<title>Behind Enemy Lines</title>
<title>Narnia- The Legend</title>
4. Namespace Node
Namespaces are being used in the well-formed XML document which is assigned by the element and attribute names. This declaration is assigned at the beginning of the document where the root element is about to declare.
In the above code two namespaces are assigned for the XML element ‘details ‘and the child elements of this name take the respective prefix. If the same namespace is declared by the element or attribute it is assigned implicitly. If it uses different namespaces it is done explicitly with the prefix ‘xmlns’. And finally, this namespace is equivalent to the namespace node in the respective tree format.
<details xmlns:airline="http://localhost/XML/delivery" xmlns:bookinginfo="http://localhost/XML/bookinginfo">
5. Instruction Node
This processing instruction makes documents contain some instructions for certain applications with the content of some string values. This processing Instruction format starts with the “<? “and ends up with “?>” prolog. In terms of Xpath expression Processing Instruction node are located outside the tree model and selects any global assignment like XSL-stylesheet and has an expanded name too.
<?xml-stylesheet type="text/css" href="style.css"?>
6. Comment Node
In XPath, the comment node specifies the comments in the corresponding XML document application. In this scenario, this node picks only the character content and not the mark-up tags. This node doesn’t have any expanded name.
Ticket is not endorsable
7. Root Node
This node in Xpath serves as a first node in the document tree. And every element of the Document appears under this.
<travel date="2018-03-04">
Therefore, all these referencing of nodes helps in traversing to reach the specific nodes in the Document.
Conclusion
Therefore, in this article we have seen the details of nodes and how XPath handles and defines them in an XML document. This is an introductory concept that will walk through how the different types of nodes go through the document with the basic concepts and an example. We also learned the types and their implementation in XPath expression which can be used in the domain of Software.
Recommended Articles
This is a guide to XPath Nodes. Here we discuss an introduction to XPath Nodes with seven different types of nodes and examples. You can also go through our other related articles to learn more –