diff --git a/Transformation/ContextFunctions.cs b/Transformation/ContextFunctions.cs index c9a4980..b4ef284 100644 --- a/Transformation/ContextFunctions.cs +++ b/Transformation/ContextFunctions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Sprache; using XNeedle.Parser; using XNeedle.Parser.Elements; @@ -45,6 +46,28 @@ namespace XNeedle.Transformation return txt; } + public static string cdata(Context ctx) + { + if (ctx.Node is null) return ""; + var cdataNode = ctx.Node.Nodes().OfType().FirstOrDefault(); + return cdataNode?.Value ?? ""; + } + + public static string cdata(Context ctx, string txt) + { + if (ctx.Node is null) return ""; + var cdataNode = ctx.Node.Nodes().OfType().FirstOrDefault(); + if (cdataNode != null) + { + cdataNode.Value = txt; + } + else + { + ctx.Node.Add(new System.Xml.Linq.XCData(txt)); + } + return txt; + } + public static string attr(Context ctx, string name, string value) { if (ctx.Node is null) return "";