Skip to content

Commit aa64598

Browse files
committed
refactor(XMLDiff): improve add operation logic to exclude numeric ID patterns if possible
1 parent dc92ca1 commit aa64598

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

XMLDiff/Program.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace X4XmlDiffAndPatch
1212
class XMLDiff
1313
{
1414
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
15+
private static readonly Regex NumericIdsPattern = new Regex(@"\[\d+\]");
1516

1617
static void Main(string[] args)
1718
{
@@ -646,11 +647,18 @@ private static bool CompareElements(
646647
XElement addOp;
647648
if (i > 0)
648649
{
649-
addOp = new XElement(
650-
"add",
651-
new XAttribute("sel", GenerateXPath(originalChildren[i - 1], pathOptions)),
652-
new XAttribute("pos", "after")
653-
);
650+
string xpath = GenerateXPath(originalChildren[i - 1], pathOptions);
651+
string pos = "after";
652+
if (NumericIdsPattern.IsMatch(xpath))
653+
{
654+
string xpathBefore = GenerateXPath(originalChild, pathOptions);
655+
if (!NumericIdsPattern.IsMatch(xpathBefore))
656+
{
657+
xpath = xpathBefore;
658+
pos = "before";
659+
}
660+
}
661+
addOp = new XElement("add", new XAttribute("sel", xpath), new XAttribute("pos", pos));
654662
}
655663
else
656664
{

0 commit comments

Comments
 (0)