XPath concat Example
We studied aboutXpath normalize-space() method in theprevious example. In this example, we will study how theconcat(String s1, String s2, String... s3) method works.
TheXPath-concat method works similar to theString#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 moreXPathExpression to evaluate. The output from theXPathExpressionis then concatenated and the resulting string is returned.
We will look at an example to see how theconcat method works in practice:
Consider theXML file below:
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>Batsman</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>
Now we willconcat the attributes of two cricketers using theXPath concat method.
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 concat exampleXPathExpression 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);}}Output:
The concat result is : rightylefty
As you can see in the output, the result of the two Xpath expressions are concatenated producing output asrightylefty.
We can alsoconcat static string with output of anXpathExpression 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);System.out.println(result);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 Xpathconcat(String s1, String s2, String... s3) method works.
You can download the source code of this example here:XPathConcatDemo.zip

Thank you!
We will contact you soon.



