Updated February 14, 2023
Introduction to XPath Count
Xpath count function is used to count the number of nodes from the set of nodes, which will return the integer value. The function contains the argument as a node-set, which will return the number representing nodes in the node-set. This function counts the number of nodes from the given xpath expression, which matched our node set.
What is XPath Count?
The count function in xpath will count all objects retrieved by the enclosed query and then return the value as an integer. The syntax of the xpath count function is straightforward; it will only contain the node-set argument. If suppose we want to count the nodes which were present in the message, then same time, we can use the function. The function is used to read and set the values inside our message; for setting the values in our message, we require an inside message to set the shape of the construct.
How to Use XPath Count?
The below steps show how to use the xpath count function as follows.
In the below example, we are creating the project of XpathCount. In the below example, we are creating a template of the project name as xpath count into spring boot.
1. While creating the project into the xpath count using spring boot, we are providing the group’s name as com.example, artifact name as XpathCount, project name as XpathCount, and selected java version as 11.
Group – com.example
Artifact name – XpathCount
Name – XpathCount
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XpathCount
Project Description – Project for XpathCount
Dependencies – Spring web
Packaging – Jar
2. In the figure below, we can extract the project name as XpathCount and open this project using the spring tool suite.
3. In the below step, we check the project’s structure. Also, we are seeing the project dependency, which was added to the pom.xml file. Also, we are checking all the structures of the example project.
4. Below, we are adding the xpath dependency into the XpathCount project. We need to add the dependency as the tag of dependency.
Code:
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-xpath </artifactId>
<version> 8.7 </version>
</dependency>
Output:
5. Now, we are creating the xml file name stud.xml to define the use of the function as follows.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<Stud>
<StudId> 1 </StudId>
<Name>
<StudName> ABC </StudName>
<Surname> XYZ </Surname>
</Name>
<Addr>
<Stud_addr> Pune </Stud_addr>
</Addr>
</Stud>
<Stud>
<StudId> 1 </StudId>
<Name>
<StudName> PQR </StudName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Stud_addr> Mumbai </Stud_addr>
</Addr>
</Stud>
<Stud>
<StudId> 1 </StudId>
<Name>
<StudName> PQR </StudName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Stud_addr> Mumbai </Stud_addr>
</Addr>
</Stud>
</Student>
Output:
6. After creating the stud.xml file, we are now creating the java file for counting the number of students. In the below example, we are creating a class name as xpath_count as follows.
Code:
public class xpath_count
{
DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance ();
DBF.setNamespaceAware (true);
DocumentBuilder DB = DBF.newDocumentBuilder ();
org.w3c.dom.Document xpath_doc = DB.parse ("stud_info.xml");
XPathFactory xf = XPathFactory.newInstance ();
XPath xpath_count = xf.newXPath ();
XPathExpression xpath_expr = xpath_count.compile ("count (//students/student)");
Number res = (Number) xpath_expr.evaluate (xpath_doc, XPathConstants.NUMBER);
System.out.println ("The student info" +res);
}
}
Output:
XPath Count Function
The xpath function counts the node number from the set of nodes. Below is the syntax of the function as follows.
Syntax:
Count (node-set)
The function contains the argument as a node-set; the parameter node-set is nothing but the set of nodes used to count the node. The function also returns an integer representing the set of nodes.
Below is the primary example of the function which was used in xpath. Using the below count example, we are retrieving the count of all students as follows.
Example:
Code:
Count (/student/stud)
In the above example, we can see that we are using the parent element as a student and the child element as a stud, so it will retrieve all the students as output. The count function returns the number of nodes from the set of nodes. To fulfill the need of users, we can use the xpath count function with other functions. Xpath provides several functions which were used to perform the calculations.
Example of XPath Count
Different examples are mentioned below:
To define an example, we are using the XpathCount project template. In the below example, we are creating the emp.xml file as follows. In that file, we are defining the info of employees as follows.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
<Emp>
<EmpId> 101 </EmpId>
<Name>
<EmpName> ABC </EmpName>
<Surname> XYZ </Surname>
</Name>
<Addr>
<Emp_addr> Pune </Emp_addr>
</Addr>
</Emp>
<Emp>
<EmpId> 102 </EmpId>
<Name>
<EmpName> PQR </EmpName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Emp_addr> Mumbai </Emp_addr>
</Addr>
</Emp>
<Emp>
<EmpId> 103 </EmpId>
<Name>
<EmpName> PQR </EmpName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Emp_addr> Mumbai </Emp_addr>
</Addr>
</Emp>
<Emp>
<EmpId> 104 </EmpId>
<Name>
<EmpName> PQR </EmpName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Emp_addr> Mumbai </Emp_addr>
</Addr>
</Emp>
<Emp>
<EmpId> 105 </EmpId>
<Name>
<EmpName> PQR </EmpName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Emp_addr> Mumbai </Emp_addr>
</Addr>
</Emp>
</Employee>
Output:
After creating the emp.xml file, we are creating the java file to define the function example as follows. We are creating the class name as xpath_count as follows:
Code:
public class xpath_count
{
DocumentBuilderFactory xpath_dbf = DocumentBuilderFactory.newInstance ();
xpath_dbf.setNamespaceAware (true);
DocumentBuilder xpath_db = xpath_dbf.newDocumentBuilder ();
org.w3c.dom.Document xpath_doc = xpath_db.parse ("emp_info.xml");
XPathFactory xpath_xf = XPathFactory.newInstance();
XPath xpath_count = xpath_xf.newXPath();
XPathExpression xpath_expr = xpath_count.compile ("count (//Employee/emp)");
Number res = (Number) xpath_expr.evaluate (xpath_doc, XPathConstants.NUMBER);
System.out.println ("The Employee info" +res);
}
}
Output:
Conclusion
The count function in xpath will count all objects retrieved by the enclosed query and then return the value as an integer. The function counts the number of nodes from the set of nodes and will return the integer value.
Recommended Articles
This is a guide to XPath Count. Here we discuss the introduction and how to use XPath count with function and example. You may also have a look at the following articles to learn more –