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 :


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

	<cricketer type="righty">
		<name>MS Dhoni</name>
	<cricketer type="lefty">
		<name>Shikhar Dhawan</name>
		<role>               Bats           man     </role>
	<cricketer type="righty">
		<name>Virat Kohli</name>
	<cricketer type="righty">
	<cricketer type="lefty">
		<name>Zaheer Khan</name>

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,


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();
				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);



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.


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

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

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.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button