Here you can find the source of difference(String xpath1, String xpath2)
public static String difference(String xpath1, String xpath2)
//package com.java2s; //License from project: Open Source License public class Main { public static String difference(String xpath1, String xpath2) { String[] paths1 = split(xpath1); String[] paths2 = split(xpath2); int length = Math.min(paths1.length, paths2.length); int index = 0; while (index < length && paths1[index].equals(paths2[index])) { index++;// ww w. ja v a2 s. c o m } StringBuffer b = new StringBuffer(); for (int i = 0; i < index; i++) { b.append(paths1[i]); } while (index < paths1.length) { b.append(paths1[index++]); b.append('/'); } return b.toString(); } public static String[] split(String xpath) { String[] paths = xpath.split("/"); if (paths.length == 0) { paths = new String[1]; paths[0] = ""; } return paths; } }