Skip to content

Commit a04494d

Browse files
committed
fix(XMLDiff): fix skipping valid xpath step in case if "//" doesn't work with it
fix(XMLDiff): fix losing the previously gathered path if some parent is accessible via "//" fix(XMLDiff): in total fixes #10
1 parent 31e52c4 commit a04494d

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

XMLDiff/Program.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,16 @@ private static (string step, string patchForParent) GetElementPathStep(
662662
string step = "";
663663
if (patchForParent == "")
664664
patchForParent = $"{element.Name.LocalName}";
665-
var attributes = element.Attributes().ToList();
665+
List<XAttribute> attributes = [];
666+
IEnumerable<XElement> matches = parent.XPathSelectElements(patchForParent);
667+
if (matches.Count() == 1 && matches.First() == element)
668+
{
669+
step = patchForParent;
670+
}
671+
else
672+
{
673+
attributes = element.Attributes().ToList();
674+
}
666675
if (attributes.Count > 0)
667676
{
668677
if (pathOptions.UseAllAttributes)
@@ -671,9 +680,16 @@ private static (string step, string patchForParent) GetElementPathStep(
671680
foreach (var attr in attributes)
672681
{
673682
string attrValue = attr.Value.Replace("\"", "&quot;");
674-
xpath += $"[@{attr.Name.LocalName}=\"{attrValue}\"]";
683+
if (attrValue.Contains("'"))
684+
{
685+
xpath += $"[@{attr.Name.LocalName}=\"{attrValue}\"]";
686+
}
687+
else
688+
{
689+
xpath += $"[@{attr.Name.LocalName}='{attrValue}']";
690+
}
675691
}
676-
var matches = parent.XPathSelectElements(xpath);
692+
matches = parent.XPathSelectElements(xpath);
677693
if (matches.Count() == 1 && matches.First() == element)
678694
{
679695
step = xpath;
@@ -688,8 +704,16 @@ private static (string step, string patchForParent) GetElementPathStep(
688704
foreach (var attr in attributes)
689705
{
690706
string attrValue = attr.Value.Replace("\"", "&quot;");
691-
string xpath = $"{patchForParent}[@{attr.Name.LocalName}=\"{attrValue}\"]";
692-
var matches = parent.XPathSelectElements(xpath);
707+
string xpath = $"{patchForParent}";
708+
if (attrValue.Contains("'"))
709+
{
710+
xpath += $"[@{attr.Name.LocalName}=\"{attrValue}\"]";
711+
}
712+
else
713+
{
714+
xpath += $"[@{attr.Name.LocalName}='{attrValue}']";
715+
}
716+
matches = parent.XPathSelectElements(xpath);
693717
if (matches.Count() == 1 && matches.First() == element)
694718
{
695719
if (step == "")
@@ -698,7 +722,13 @@ private static (string step, string patchForParent) GetElementPathStep(
698722
{
699723
var doc_matches = doc.XPathSelectElements($"//{xpath}");
700724
if (doc_matches.Count() == 1)
725+
{
701726
return ($"//{xpath}", patchForParent);
727+
}
728+
else
729+
{
730+
return (xpath, patchForParent);
731+
}
702732
}
703733
}
704734
}
@@ -710,6 +740,10 @@ private static (string step, string patchForParent) GetElementPathStep(
710740
{
711741
return ($"//{patchForParent}", patchForParent);
712742
}
743+
else
744+
{
745+
return (patchForParent, patchForParent);
746+
}
713747
}
714748
return (step, patchForParent);
715749
}
@@ -736,7 +770,8 @@ private static string GenerateXPath(XElement element, PathOptions pathOptions)
736770
(string step, string patchForParent) = GetElementPathStep(current, parent, doc, pathOptions);
737771
if (step.StartsWith("//"))
738772
{
739-
return step;
773+
path.Insert(0, step);
774+
return path.ToString();
740775
}
741776
if (step == "")
742777
{

0 commit comments

Comments
 (0)