Updated March 28, 2023
Definition of XPath local-name
XPath local-name is a function which is returning the string name for the first node in a given set of nodes. Basically, XPath local-name will be returning the local part of the name as an argument string which is either the zero-length which is contained in a form of lexical. The form of zero-argument is deterministic, focus dependent, and also contains the context-dependent into the XPath.
Overview of XPath local-name
XPath local-name function will be returning the string. If the argument is omitted in XPath local-name it will default to the items of context. The function behavior of the XPath local name is omitted arguments is same as context item which is passed into the argument. After supplying the argument the empty sequence of functions will return the string of zero length. After identifying the node $arg argument the function will return the length of the zero string. Then otherwise this function will be returning the local part of the qname which was expanded and the node is identified by using the argument.
How to use XPath local-name?
- As we know that XPath local name function will return the string which was representing the node-set function name.
- Below is the syntax of XPath local-name which shows how we can use the XPath local name.
Syntax –
Local-name([set-of-node])
- In the above syntax node-set is the optional parameter, and the local name into the first node-set is returning the node-set value. If this argument will be omitted then the context of the current node is used.
- The below steps show how we can use the XPath local name as follows. We are creating the project name as XPathLocalName.
1. In this step, we are creating the project template of XPath local-name in spring boot. We have provided project group name as com.example, artifact name as XPathLocalName, project name as XPathLocalName, and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – XPathLocalName
Name – XPathLocalName
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathLocalName
Project Description – Project for XPathLocalName
Dependencies – Spring web
Packaging – Jar
2. In this step we are extracting the downloaded project and opening the same by using the spring tool suite as follows.
3. In this step we are checking all the project structures and their files are as follows. Also, we are checking whether that pom.xml file is created or not. Suppose this file is not created then we need to create the same manually. In the below example this file is created, so we have no need to create it manually.
4. In this step we are adding the dependency in XPath local name project. We are adding a dependency.
Code –
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
5. After creating the structure of project in this step we are creating the stud.xml file are as follows.
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<cr:students xmlns:cr = "example.com/">
<cr:student std = 1>
<stud_name> ABC </stud_name>
<addr> Pune </addr>
</cr:student>
<cr:student std = 2>
<stud_name> PQR </stud_name>
<addr> Mumbai </addr>
</cr : student>
<cr:student std = 3>
<stud_name> XYZ </stud_name>
<addr> Pune </addr>
</cr : student>
<cr : student type = 4>
<stud_name> BCD </stud_name>
<addr> Pune </addr>
</cr : student>
<cr : student std = 5>
<stud_name> CBD </stud_name>
<addr> Chennai </addr>
</cr : student>
</cr : students>
6. After creating the stud.xml file now we are creating the local name function are as follows.
Code –
public class XPath_localname
{
DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance ();
DBF.setNamespaceAware (true);
DocumentBuilder DB = DBF.newDocumentBuilder ();
Document doc = (Document) DB.parse ("stud.xml");
XPathFactory XF = XPathFactory.newInstance();
XPath XPath = XF.newXPath();
XPathExpression xexpr = XPath.compile ("//*[local-name ()='student']/stud_name/text()");
String type = (String) xexpr.evaluate (doc, XPathConstants.STRING);
System.out.println ("Name of student : " + type);
}
}
XPath local-name function
XPath local name provides the node set function. Basically XPath contains the following function and XPath local-name contains the node-set function in it.
- Node sets
- Strings
- Booleans
- Numbers
At the time of working with the node-set function, the current node-set is passed as a parameter. Below is describing the node-set function as follows.
- Number last () – This function is used to return the last item from the node-set which was current.
Example –
[last ()]- Number position () – This function is used to return a current item from the node-set which was current.
Example –
[position () < 5]- Number count (node-set) – This function is used to return a number of items from the node-set argument.
Example –
Count (article)
- Node set id (object) – It will return the element with the ID which was specified.
- String local-name (node-set) – It is used to return the non-namespace portion of the node.
- String namespace-uri (node-set) – It will return the URI node name from the node-set.
- String name (node-set) – It is used to return the complex structure node name in XPath local name.
- Node set evaluate (string) – It will return a node-set result from the expression of XPath which is defined in the argument.
- Node-set instance (string) – It will returning a content of instance of named xml.
Below is the example function of XPath local name.
Code –
<Employee>
<Organization>
<EmpName> ABC </EmpName>
</Organization>
</Employee>
Examples
- Below is the example of XPath local name as follows. In below we are creating emp.xml file as follows.
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<cr:Employees xmlns:cr = "example.com/">
<cr:Employee ID = 1>
<Emp_name> ABC </Emp_name>
<addr> Pune </addr>
</cr:Employee>
<cr:Employee ID = 2>
<Emp_name> PQR </Emp_name>
<addr> Mumbai </addr>
</cr:Employee>
</cr:Employees>
- Now we are creating the XPath local name function as follows.
Code –
public class XPath_localname
{
…..
}
Conclusion
XPath local-name function will be returning the string. If the argument is omitted in XPath local-name it will default to the items of context. XPath local-name is a function which is returning the string name for the first node in a given set of nodes.
Recommended Articles
This is a guide to XPath local-name. Here we discuss the Definition, overviews, How to use XPath local-name? and examples with code implementation respectively. You may also have a look at the following articles to learn more –