xn/Transformation/Rule.cs

41 lines
1016 B
C#

using System.Collections.Generic;
using System.Xml;
using XNeedle.Parser.Elements;
namespace XNeedle.Transformation
{
public class Rule : Part
{
public required string XPathSelector { get; set; }
public required IEnumerable<Part> Body;
public void Transform(XmlDocument xml)
{
var context = new Context();
context.Node = xml.DocumentElement;
Operate(context);
}
public override void Operate(Context ctx)
{
if(ctx.Node is null)
{
return;
}
var nodes = ctx.Node.SelectNodes(XPathSelector);
if(nodes is null)
{
return;
}
foreach (XmlNode n in nodes)
{
var loopCtx = ctx.Clone();
loopCtx.Node = n;
foreach (var b in Body)
{
b.Operate(loopCtx);
}
}
}
}
}