XPath concat Example

We studied about Xpath normalize-space() method in the previous example. In this example, we will study how the concat(String s1, String s2, String... s3) method works.

The XPath-concat method works similar to the String#concat method. It joins two or more strings into a single string. The argument strings maybe two or more static strings and/or they may be two or more XPathExpression to evaluate. The output from the XPathExpressionis then concatenated and the resulting string is returned.

We will look at an example to see how the concat method works in practice:

Consider the XML file below:


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

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

Now we will concat the attributes of two cricketers using the XPath concat method.


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 concat example
				XPathExpression expr = xpath.compile("concat(//cricketer[name='Shami']/@type,//cricketer[name='Zaheer Khan']/@type)");
				String combination = (String) expr.evaluate(doc, XPathConstants.STRING);
				System.out.println("The concat result is : " + combination);



The concat result is : rightylefty

As you can see in the output, the result of the two Xpath expressions are concatenated producing output as rightylefty.

We can also concat static string with output of an XpathExpression or two static strings themselves. The snippet below demonstrates this:

XPathExpression expr = xpath.compile("concat('The handed-ness of ZKhan is : ',//cricketer[name='Zaheer Khan']/@type)");
String result = (String) expr.evaluate(doc, XPathConstants.STRING);

In the above code-snippet, the concat method has:

 The handed-ness of ZKhan is : lefty

Download the source code

Here we studied how the Xpath concat(String s1, String s2, String... s3) method works.

You can download the source code of this example here: XPathConcatDemo.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