public static string RowCommonPart(string leftPath, string rightPath) { string root = CommonPart(leftPath, rightPath); XPathInfo left = new XPathInfo(Relative(leftPath, root)); XPathInfo right = new XPathInfo(Relative(rightPath, root)); if (left.Parts.Length > 0 && right.Parts.Length > 0) { string leftPart = PathWithoutBracets(left.Parts[0]); if (!string.IsNullOrEmpty(leftPart) && leftPart == PathWithoutBracets(right.Parts[0])) return root + "/" + leftPart; } return root; }
public static XPathInfo Parse(string xpath) { lock (cache) { if (!cache.ContainsKey(xpath)) cache[xpath] = new XPathInfo(xpath); return cache[xpath]; } }
public static string Relative(string nodePath, string rootPath) { XPathInfo node = new XPathInfo(nodePath); XPathInfo root = new XPathInfo(rootPath); if (node.Parts.Length >= root.Parts.Length) { int i; for (i = 0; i < root.Parts.Length - 1; i++) if (node.Parts[i] != root.Parts[i]) return null; if (node.Parts[i].StartsWith(root.Parts[i])) i++; else return null; string result = string.Join("/", node.Parts, i, node.Parts.Length - i); if (!string.IsNullOrEmpty(result)) return result; else return "."; } else return null; }
public static string CommonPart(string leftPath, string rightPath) { XPathInfo left = new XPathInfo(leftPath); XPathInfo right = new XPathInfo(rightPath); //StringBuilder result = new StringBuilder(); int i; List<string> result = new List<string>(); for (i = 0; i < Math.Min(left.Parts.Length, right.Parts.Length); i++) { if (left.Parts[i] == right.Parts[i]) result.Add(left.Parts[i]); else { string lShort = PathWithoutBracets(left.Parts[i]); string rShort = PathWithoutBracets(right.Parts[i]); //if (lShort == rShort) // result.Add(lShort); if (lShort == right.Parts[i]) result.Add(lShort); else if (rShort == left.Parts[i]) result.Add(rShort); break; } } if (i > 0) { return string.Join("/", result.ToArray()); } else return "."; }