xn/Transformation/Rule.cs

43 lines
1.0 KiB
C#

using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
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(XDocument xml)
{
var context = new Context();
context.Node = xml.Root;
Operate(context);
}
public override void Operate(Context ctx)
{
if(ctx.Node is null)
{
return;
}
var nodes = ctx.Node.XPathSelectElements(XPathSelector);
if(nodes is null)
{
return;
}
foreach (XElement n in nodes)
{
var loopCtx = ctx.Clone();
loopCtx.Node = n;
foreach (var b in Body)
{
b.Operate(loopCtx);
}
}
}
}
}