Home » Core Java » xml » XPath » XPath normalize-space example

About Chandan Singh

Chandan Singh
Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.

XPath normalize-space example

In this example, we will talk about the normalize-space method in XPath.

The normalize-space(String s) method is used to normalize a string i.e. to remove any leading or trailing spaces from the string s passed as parameter to the XPath function.

We will look at a few examples at the examples to see how the normalize-space method works in an XpathExpression
 
Consider the following XML File :

cricketTeam_info.xml:


<?xml version="1.0" encoding="UTF-8"?>

<cricketers>
	<cricketer type="righty">
		<name>MS Dhoni</name>
		<role>Captain</role>
		<position>Wicket-Keeper</position>
	</cricketer>
	<cricketer type="lefty">
		<name>Shikhar Dhawan</name>
		<role>               Bats           man     </role>
		<position>Point</position>
	</cricketer>
	<cricketer type="righty">
		<name>Virat Kohli</name>
		<role>Batsman</role>
		<position>cover</position>
	</cricketer>
	<cricketer type="righty">
		<name>Shami</name>
		<role>Bowler</role>
		<position>SquareLeg</position>
	</cricketer>
	<cricketer type="lefty">
		<name>Zaheer Khan</name>
		<role>Bowler</role>
		<position>FineLeg</position>
	</cricketer>
</cricketers>

In the above XML file, in the second cricketer Node we have purposely introduced lots of spaces in the Role node. However, I now want the output such that there is no unwanted space in the string I get, after executing the XpathExpression.

To achieve that output, we will use the normalize-space(String s) method. Let’s see the java code for that,

XpathNormalizeSpaceDemo.java:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;


public class XpathSubStringDemo
{
		public static void main(String[] args) throws Exception
		{

				DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
				documentBuilderFactory.setNamespaceAware(true);
				DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
				Document doc = documentBuilder.parse("src/cricketTeam_info.xml");

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

				//XPath normalize-space example
				XPathExpression expr = xpath.compile("normalize-space(//cricketer[name='Shikhar Dhawan']/role/text())");
				String result = (String) expr.evaluate(doc, XPathConstants.STRING);
				System.out.println("The normalized role-string is : " + result);

		}
}

Output:


The normalized role-string is : Bats man

As you can see in the output, the extra spaces in the Node role have been removed and we get the normalized string as the output. The normalize-space method completely strips off the spaces from the beginning and end of the string. If, there are multiple spaces in between the string, the method replaces spaces with a single space.

Conclusion:

Here we studied about the XPath normalize-space example and how to use the same.

Download
You can download the source code of this example here: XpathNormalizeSpaceDemo.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns