Catrobat.IDE.Core.Utilities.Helpers.XPathHelper.GetXPath C# (CSharp) Метод

GetXPath() публичный статический Метод

public static GetXPath ( System.Xml.Linq.XElement fromElement, System.Xml.Linq.XElement toElement ) : string
fromElement System.Xml.Linq.XElement
toElement System.Xml.Linq.XElement
Результат string
        public static string GetXPath(XElement fromElement, XElement toElement)
        {
            var path = "";
            
            var absolutePathFrom = GetAbsolutePath(fromElement);
            var absolutePathTo = GetAbsolutePath(toElement);

            var commonPath = 0;
            while (commonPath < absolutePathFrom.Count && commonPath < absolutePathTo.Count && absolutePathFrom[commonPath] == absolutePathTo[commonPath])
                commonPath++;

            for (var i = 0; i < absolutePathFrom.Count - commonPath + 1; i++)
                path += "../";

            for (var i = commonPath; i < absolutePathTo.Count; i++)
            {
                path += GetNameWithIndex(absolutePathTo[i]) + "/";
            }

            path += GetNameWithIndex(toElement);

            //path = path.Replace("[1]", "");

            return path;
        }

Usage Example

 public static void SetReferenceTarget(this XElement element, XElement target)
 {
     element.SetAttributeValue(ReferenceAttributeName, XPathHelper.GetXPath(element, target));
 }