Use of string-length() function in XPath

In this section we will discuss about string-length() function in XPath. string-length() function returns the number of character in a string and you can use "=","<" and ">" for comparing two numbers.

Use of string-length() function in XPath

Use of string-length() function in XPath

     

In this section we will discuss about string-length() function in XPath. string-length() function returns the number of character in a string and you can use "=","<" and ">" for comparing two numbers.

In this example we have created an XML file "persons.xml" first, which is necessary to execute XPath query on to it. This "persons.xml" contains information related to name, age, gender of different persons.

Here is the full source code for persons.xml file as follows :

persons.xml

<?xml version="1.0" ?>
<information>
  <person id="1">
  <name>Deep</name>
  <age>34</age>
  <gender>Male</gender>
  </person>
 
 <person id="2">
  <name>Kumar</name>
  <age>24</age>
  <gender>Male</gender>
  </person>
 
  <person id="3">
  <name>Deepali</name>
  <age>19</age>
  <gender>Female</gender>
  </person>

  <!-- more persons... -->
</information>

Now we have declared a class XPathString  and in this class we are parsing the XML file with JAXP. First of all we need to load the document into DOM Document object. We have put that persons.xml file in that current working directory.

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();

  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("persons.xml");

Above lines of code parses "persons.xml" file and creates a Document object. Next we have created XPath object with the use of XPathFactory.

XPath xpath = XPathFactory.newInstance().newXPath();

Now we compile the path with the use of compile() method. Finally we will evaluate XPath expression .
Expression "/*[string-length(name()) = 11]" will select elements with eleven characters name, "//*[string-length(name()) < '4']" will select elements with less than four characters name and  "//*[string-length(name()) > '4']" will select elements with greater than four characters name.

Here is the full example code for XPathString class:

XPathString.java

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathString {

  public static void main(String[] args
 throws ParserConfigurationException, SAXException, 
  IOException, XPathExpressionException {

  DocumentBuilderFactory domFactory = 
   DocumentBuilderFactory.newInstance
();
  domFactory.setNamespaceAware(true)
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("persons.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = 
   xpath.compile
("/*[string-length(name()) = '11']");
 //Select elements which have string length equals to 11
    System.out.println("Select elements which 
  have string length equals to 11"
);
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }  
    System.out.println("Select elements which 
  have string length greater than 4"
);
  expr = xpath.compile("//*[string-length(name()) > '4']");
   //Select elements which have string length greater than 4
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }
    System.out.println("Select elements which 
  have string length less than 4");
  expr = xpath.compile(".//*[string-length(name()) < '4']");
   //Select elements which have string length less than 4
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }  
  }
}

Output:


Download Source Code