Path: redb-route/redb.Route/Xslt/XslCompiledTransformEngine.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Xslt/XslCompiledTransformEngine.cs ---- using System.Xml; using System.Xml.Linq; using System.Xml.XPath; using System.Xml.Xsl; namespace redb.Route.Xslt; /// /// Built-in backed by the BCL /// (System.Xml.Xsl) — zero external dependencies, XSLT 1.0 (like Apache Camel's default /// JAXP engine; use a Saxon-backed engine for 2.0/3.0). The stylesheet is compiled once at construction /// and the compiled template is reused for every transform (the compilation is the expensive part). /// /// The compiled transform is loaded with — inline script and /// the document() function are disabled, which is the safe default for stylesheets coming from /// outside the application. /// /// public sealed class XslCompiledTransformEngine : IXsltEngine { private readonly XslCompiledTransform _compiled; private XslCompiledTransformEngine(XslCompiledTransform compiled) => _compiled = compiled; /// /// Compiles the engine from a stylesheet file (or URL). The path is used as the base URI, so /// xsl:import / xsl:include resolve relative to the stylesheet. /// public static XslCompiledTransformEngine FromFile(string stylesheetPath) { ArgumentException.ThrowIfNullOrWhiteSpace(stylesheetPath); var xslt = new XslCompiledTransform(); xslt.Load(stylesheetPath); return new XslCompiledTransformEngine(xslt); } /// /// Compiles the engine from an inline stylesheet document. Self-contained stylesheets only — /// xsl:import/xsl:include have no base URI to resolve against. /// public static XslCompiledTransformEngine FromContent(string stylesheetXml) { ArgumentException.ThrowIfNullOrWhiteSpace(stylesheetXml); var xslt = new XslCompiledTransform(); using var reader = XmlReader.Create(new StringReader(stylesheetXml)); xslt.Load(reader); return new XslCompiledTransformEngine(xslt); } /// public object Transform(object? body, XsltOutput output, IReadOnlyDictionary? parameters) { using var input = CreateInputReader(body); var args = new XsltArgumentList(); if (parameters is not null) { foreach (var (name, value) in parameters) { if (value is null) continue; // Names that aren't valid XSLT parameter identifiers (e.g. a header like "Content-Type" // is fine, but arbitrary keys may not be) are skipped rather than failing the transform — // matching Camel, which silently ignores headers that aren't declared/usable as params. try { args.AddParam(name, string.Empty, value); } catch (ArgumentException) { /* not a usable parameter name — skip */ } } } // Using the TextWriter / Stream overloads lets XslCompiledTransform honour the stylesheet's // xsl:output method (xml / html / text) and encoding automatically. switch (output) { case XsltOutput.Bytes: { using var ms = new MemoryStream(); _compiled.Transform(input, args, ms); return ms.ToArray(); } case XsltOutput.Dom: { var doc = new XmlDocument(); using var writer = doc.CreateNavigator()!.AppendChild(); _compiled.Transform(input, args, writer); writer.Close(); return doc; } default: // String { using var sw = new StringWriter(); _compiled.Transform(input, args, sw); return sw.ToString(); } } } private static XmlReader CreateInputReader(object? body) => body switch { XmlReader reader => reader, string s => XmlReader.Create(new StringReader(s)), byte[] bytes => XmlReader.Create(new MemoryStream(bytes)), Stream stream => XmlReader.Create(stream), XNode node => node.CreateReader(), // XDocument / XElement (System.Xml.Linq) IXPathNavigable navigable => navigable.CreateNavigator()!.ReadSubtree(), _ => throw new NotSupportedException( $"XSLT input body type '{body?.GetType().Name ?? "null"}' is not supported. " + "Provide XML as string, byte[], Stream, XmlReader, XDocument/XElement, or IXPathNavigable."), }; }