XPath substring example
In this example, we will try to see how we can use thesub-string method in theXPath for our use-cases.
Thesub-string method is used to search for the sub-string in the beginning or the end or anywhere in theXPath node. It can also be used to extract the part of the sub-string from the string by specifying the beginning index and end index.
TheXPath provides following methods for this :
substring-before(String target, String tosearch)substring-after(String target, String tosearch)substring(String target, int startingindex, int length)
We will look at all the methods in brief and see how to use each of them.
cricketTeam_info.java:
<?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>
1. substring(String target, int startingindex, int length)
Substring(target, startindex, length, ) method returns the sub-string of the target string from the start-index to thelength specified. If the length argument is not provided, it returns the string from thestart-index specified to the last character of the target string.
XPathExpression expr = xpath.compile("substring(//cricketer[name='MS Dhoni']/position,'1','4')");String substr = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring of Wicket is : " + substr );Output:
The substring of Wicket is : Wick
The target string here is ‘wicket-keeper‘. By passing1,4 we are extracting the characters from 1st to 4th position of the target string.
Unlike in Java, the XPath string index starts with 1.
2. substring-before(String target, String tosearch)
Thesubstring-before is used to extract the substring of the target occurring before an occurrence of the string passed as toSearch argument. Lets look at an example to understand this :
expr = xpath.compile("substring-before(//cricketer[name='MS Dhoni']/position,'-Keeper')");String substrbefore = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substrbefore);Output:
The substring before Keeper is : Wicket
The string occurring prior to“-Keeper” in“Wicket-Keeper” is returned by thesubstring-beforemethod.
3. substring-after(String target, String tosearch)
This method works exactly opposite of thesubstring-before method. It returns the substring after the tosearch string till the end of the target string.
expr = xpath.compile("substring-after(//cricketer[name='MS Dhoni']/position,'Wicket-')");String substrafter = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring after Keeper is : " + substrafter);Output:
The substring after Keeper is : Keeper
Thesubstring-after returns the substring after the “Wicket“, which is “keeper“.
XpathSubStringDemo.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 subtring exampleXPathExpression expr = xpath.compile("substring(//cricketer[name='MS Dhoni']/position,'1','4')");String substr = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substr);// XPath subtring-before exampleexpr = xpath.compile("substring-before(//cricketer[name='MS Dhoni']/position,'-Keeper')");String substrbefore = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring before Keeper is : " + substrbefore);// XPath subtring-after exampleexpr = xpath.compile("substring-after(//cricketer[name='MS Dhoni']/position,'Wicket-')");String substrafter = (String) expr.evaluate(doc, XPathConstants.STRING);System.out.println("The substring after Keeper is : " + substrafter);}}4. Conclusion:
Here we studied the sub-string method and how to use them.
You can download the source code of this example here:XpathSubStringDemo.zip

Thank you!
We will contact you soon.



