XPath

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

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

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

0 Comments
Inline Feedbacks
View all comments
Back to top button