Path: redb-route/redb.Route/Expressions/XPathExpression.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/Expressions/XPathExpression.cs ---- using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Predicates; namespace redb.Route.Expressions; /// /// Expression for evaluating XPath queries against the message body in an . /// /// /// Extracts data from an XML document using W3C XPath 1.0 expressions. /// Supports , , string XML, and POCO bodies /// (auto-serialized to XML). Optionally accepts an /// for querying namespace-qualified documents. /// public class XPathExpression : Expression, IPredicateExpression { private readonly string _xpath; private readonly string _effectivePath; private readonly IXmlNamespaceResolver? _namespaceResolver; private readonly XPathResult _result; private readonly IExpression? _source; private readonly bool _trim; private readonly IReadOnlyList<(string Name, IExpression Value)>? _parameters; /// /// Initializes a new instance of the class. /// /// The XPath expression used to extract data. /// /// Optional namespace resolver for querying documents with XML namespaces. /// /// /// The XPath-level type of the result. The default, , is /// XPath's own default. /// /// /// What to run the path against. null — the default — means the message body. /// /// Trim whitespace off extracted text. Default false. /// Thrown when is null. public XPathExpression( string xpath, IXmlNamespaceResolver? namespaceResolver = null, XPathResult result = XPathResult.NodeSet, IExpression? source = null, bool trim = false) : this(xpath, namespaceResolver, result, source, trim, null) { } private XPathExpression( string xpath, IXmlNamespaceResolver? namespaceResolver, XPathResult result, IExpression? source, bool trim, IReadOnlyList<(string Name, IExpression Value)>? parameters) { _xpath = xpath ?? throw new ArgumentNullException(nameof(xpath)); _namespaceResolver = namespaceResolver; _result = result; _source = source; _trim = trim; _parameters = parameters; // string(), number() and boolean() are XPath's own coercions, so asking the engine for // them is both shorter and more faithful than converting the node-set ourselves. _effectivePath = result switch { XPathResult.String => $"string({_xpath})", XPathResult.Number => $"number({_xpath})", XPathResult.Boolean => $"boolean({_xpath})", _ => _xpath }; } /// /// Initializes a new instance of the class with an explicit /// XPath-level result type. /// /// The XPath expression used to extract data. /// The XPath-level type of the result. public XPathExpression(string xpath, XPathResult result) : this(xpath, null, result) { } /// /// Returns the same expression reading instead of the message body. /// /// /// XPath("/invoice/id").From(Header("original-request")). Without it, parsing XML that /// arrived in a header means moving it into the body first — an extra step that also damages /// the body for the rest of the route. /// /// The expression producing the XML to read. /// A new expression; this one is unchanged. public XPathExpression From(IExpression source) { ArgumentNullException.ThrowIfNull(source); return new XPathExpression(_xpath, _namespaceResolver, _result, source, _trim, _parameters); } /// /// Returns the same expression with prefixes bound to namespace URIs, so a path can address a /// namespace-qualified document. /// /// /// /// XPath("/s:Envelope/s:Body").WithNamespaces(("s", "http://schemas.xmlsoap.org/soap/envelope/")). /// The expression has always accepted an , but only through /// its constructor — a route written in the DSL had no way to reach it, which made every /// namespaced document unqueryable from a route without dropping out of the DSL. /// /// /// The prefixes are the expression's own: they need not match the ones in the document, only /// the URIs must. Camel keeps namespace sets in a registry and refers to them by name /// (namespacesRef) because its XML DSL cannot write a map inline; in C# the map is /// ordinary code, so a shared set is an ordinary variable. /// /// /// Prefix/URI pairs. /// A new expression; this one is unchanged. public XPathExpression WithNamespaces(params (string Prefix, string Uri)[] namespaces) { ArgumentNullException.ThrowIfNull(namespaces); var manager = new XmlNamespaceManager(new NameTable()); foreach (var (prefix, uri) in namespaces) { ArgumentException.ThrowIfNullOrWhiteSpace(prefix, nameof(namespaces)); ArgumentException.ThrowIfNullOrWhiteSpace(uri, nameof(namespaces)); manager.AddNamespace(prefix, uri); } return new XPathExpression(_xpath, manager, _result, _source, _trim, _parameters); } /// /// Returns the same expression with whitespace trimmed off the text it extracts. /// /// /// Off by default, which is where this library has always been and differs from Camel's /// trim=true. For a single value XPath answers this itself — /// XPath("normalize-space(/order/name)") — but a node-set of several values cannot be /// normalised from inside XPath 1.0, and an indented document gives every one of them the /// surrounding whitespace. /// /// Whether to trim; the parameter exists so the option can be turned back off. /// A new expression; this one is unchanged. public XPathExpression Trimmed(bool trim = true) => new(_xpath, _namespaceResolver, _result, _source, trim, _parameters); /// /// Returns the same expression with named XPath variables bound to expressions, so a value from /// the message can take part in the query without being pasted into its text. /// /// /// /// XPath("/order[@id=$id]").WithParameters(("id", Header("orderId"))). The value is /// passed to the engine as a value: it is compared, never parsed, so a header holding /// B-2' or '1'='1 selects an order with that literal id and matches nothing, rather than /// changing what the query asks. /// /// /// The alternative is building the path by concatenation, which Camel offers as /// allowSimple — and which is the reason XPath injection has a CodeQL query and an OWASP /// entry of its own. Binding values instead is old and settled practice: JAXP's /// XPathVariableResolver, XQuery's declare variable $x external, .NET's /// XsltArgumentList, and prepared statements in SQL are all the same idea. /// /// /// The path also stays one constant string whatever the data, which is what lets a compiled /// expression be reused; concatenation defeats that by construction. /// /// /// XPath only. jpath has no equivalent, and not because of the library: RFC 9535 has no /// variables at all, so there is nothing for JsonPath.Net to expose. /// /// /// Variable names (without the $) and the expressions producing their values. /// A new expression; this one is unchanged. public XPathExpression WithParameters(params (string Name, IExpression Value)[] parameters) { ArgumentNullException.ThrowIfNull(parameters); foreach (var (name, value) in parameters) { ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(parameters)); ArgumentNullException.ThrowIfNull(value, nameof(parameters)); if (name[0] == '$') throw new ArgumentException( $"XPath parameter name '{name}' must be written without the '$': the '$' belongs " + "in the path, not in the binding.", nameof(parameters)); } var merged = _parameters is null or { Count: 0 } ? parameters : [.. _parameters, .. parameters]; return new XPathExpression(_xpath, _namespaceResolver, _result, _source, _trim, merged); } /// /// Parameters are evaluated against a message, so an evaluation that has no message cannot /// honour them — and silently dropping a binding would turn a query into a different query. /// private IExchange RequireExchange(IExchange? exchange) => exchange ?? throw new InvalidOperationException( $"XPath '{_xpath}' has bound parameters, which need an exchange to evaluate against. " + "The two-argument xpath() function of the expression language does not carry them; " + "use the expression object form."); /// /// /// Thrown when the exchange body is null or the XPath expression cannot be evaluated. /// public override T Evaluate(IExchange exchange) => ConvertResult(EvaluateRaw(exchange)); /// /// Evaluates the expression in a condition position. /// /// /// /// A node-set answers whether the path matched: XPath 1.0 reads a node-set as true exactly /// when it is non-empty, and the content of the matched nodes never enters the question. So /// Filter(XPath("/order/discount")) passes a message whose discount is 0, and an /// empty <vip/> counts as a match. /// /// /// A scalar — from string(...), count(...) > 0, or an explicit /// — is a value like any other and goes through the single DSL /// truthiness rule, so "0" is false there. To read a node's content as a /// boolean instead, ask for the CLR type: XPath<bool>("/order/flag"). /// /// /// The exchange to test. /// true when the expression holds for the exchange. public bool Matches(IExchange exchange) { var raw = EvaluateRaw(exchange); return raw is IEnumerable nodes ? nodes.Any() : RouteTruthiness.ToBoolean(raw); } /// /// Runs the XPath and returns the engine's own result: a string, a double, a bool, or an /// of nodes. /// private object EvaluateRaw(IExchange exchange) => EvaluateRawOn(ExpressionValues.ReadInput(_source, exchange), _source is not null, exchange); /// /// Runs the XPath against an already-resolved input, whatever produced it. Used by the /// expression language, where the source arrives as a value rather than as an expression. /// internal T EvaluateOn(object? input, bool fromSource) => ConvertResult(EvaluateRawOn(input, fromSource, null)); private object EvaluateRawOn(object? input, bool fromSource, IExchange? exchange) { try { var body = input; if (body == null) throw new InvalidOperationException(ExpressionValues.NoInput(fromSource, "XPath")); var node = ResolveXNode(body); // Parameters are bound per message, so the resolver they travel in is built per // evaluation; without them the expression's own namespace resolver is passed straight // through, which is the path every existing route takes. var resolver = _parameters is null or { Count: 0 } ? _namespaceResolver : XPathVariableContext.Build(_namespaceResolver, _parameters, RequireExchange(exchange)); // XPathEvaluate returns: string, double, bool, or IEnumerable (node-set) var raw = System.Xml.XPath.Extensions.XPathEvaluate(node, _effectivePath, resolver); // NODE is a node-set narrowed to its first member in document order. if (_result == XPathResult.Node && raw is IEnumerable nodes) return nodes.Take(1).ToList(); return raw; } catch (InvalidOperationException) { throw; } catch (XmlException ex) { throw new InvalidOperationException($"XML parsing error in XPathExpression: {ex.Message}", ex); } catch (Exception ex) { throw new InvalidOperationException($"Error evaluating XPath '{_xpath}': {ex.Message}", ex); } } // ── Body → XNode ── /// /// Resolves the exchange body to an suitable for XPath evaluation. /// internal static XNode ResolveXNode(object body) { if (body is XDocument xDoc) return xDoc; if (body is XElement xElem) return xElem; if (body is string s) { s = s.Trim(); if (s.Length == 0) throw new InvalidOperationException("Exchange body is an empty string. Cannot evaluate XPath expression."); return SafeXml.Parse(s); } // POCO → serialize to XML via XmlSerializer return SerializeToXDocument(body); } private static XDocument SerializeToXDocument(object body) { var serializer = new XmlSerializer(body.GetType()); using var sw = new StringWriter(); using var xw = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false }); serializer.Serialize(xw, body); return SafeXml.Parse(sw.ToString()); } // ── Result conversion ── private T ConvertResult(object raw) { // XPathEvaluate returns IEnumerable for node-set results if (raw is IEnumerable nodes) return ProcessNodeList(nodes); // Scalar results (string, double, bool) return ConvertScalar(raw); } /// /// Converts a scalar XPath result (string, double, bool) to the requested type. /// private T ConvertScalar(object value) { // A string scalar is text the same way a node's value is, so trimming applies to both. if (_trim && value is string text) value = text.Trim(); if (value is T direct) return direct; if (typeof(T) == typeof(string)) return (T)(object)(value?.ToString() ?? string.Empty); if (typeof(T) == typeof(object)) return (T)SmartConvertScalar(value); // double → numeric types if (value is double d) { if (typeof(T) == typeof(int)) return (T)(object)(int)d; if (typeof(T) == typeof(long)) return (T)(object)(long)d; if (typeof(T) == typeof(float)) return (T)(object)(float)d; if (typeof(T) == typeof(decimal)) return (T)(object)RouteDecimal.FromDouble(d); if (typeof(T) == typeof(double)) return (T)(object)d; if (typeof(T) == typeof(bool)) return (T)(object)(d != 0); } // bool → other types if (value is bool b) { if (typeof(T) == typeof(int)) return (T)(object)(b ? 1 : 0); if (typeof(T) == typeof(string)) return (T)(object)(b ? "true" : "false"); } try { return (T)RouteDecimal.ChangeType(value, typeof(T)); } catch (Exception ex) { throw new InvalidOperationException( $"Cannot convert XPath scalar result '{value}' ({value?.GetType().Name}) to {typeof(T).Name}.", ex); } } /// /// Smart conversion for Evaluate<object>: keeps original scalar type. /// private static object SmartConvertScalar(object value) { // double that is actually an integer → return int if (value is double d && d == Math.Truncate(d) && d is >= int.MinValue and <= int.MaxValue) return (int)d; return value; } // ── Node-set processing ── /// /// Processes a node-set (IEnumerable<object>) returned by XPathEvaluate. /// Nodes can be , , , /// , or . /// private T ProcessNodeList(IEnumerable nodes) { var list = nodes.ToList(); if (list.Count == 0) { if (typeof(T).IsValueType && Nullable.GetUnderlyingType(typeof(T)) == null) throw new InvalidOperationException($"No values found for XPath expression (expected non-nullable {typeof(T).Name})."); return default!; } // ── Single node ── if (list.Count == 1) return ConvertSingleNode(list[0]); // ── Multiple nodes ── return ConvertMultipleNodes(list); } /// /// Converts a single XPath node to the requested type. /// private T ConvertSingleNode(object node) { // For T=object, extract text and smart-parse (int, double, bool, or string) if (typeof(T) == typeof(object)) { var objectText = Text(node); return (T)SmartParseText(objectText); } // Return the raw node if the caller expects XElement, XAttribute, XObject, etc. if (node is T directMatch) return directMatch; var text = Text(node); if (typeof(T) == typeof(string)) return (T)(object)text; // Value types: parse from text return ParseText(text); } /// /// Converts multiple XPath nodes to the requested type. /// private T ConvertMultipleNodes(List nodeList) { var textValues = nodeList.Select(Text).ToArray(); // string → comma-joined if (typeof(T) == typeof(string)) return (T)(object)string.Join(", ", textValues); // string[] → direct if (typeof(T) == typeof(string[])) return (T)(object)textValues; // int[] if (typeof(T) == typeof(int[])) return (T)(object)textValues.Select(v => int.Parse(v)).ToArray(); // double[] if (typeof(T) == typeof(double[])) return (T)(object)textValues.Select(v => double.Parse(v, System.Globalization.CultureInfo.InvariantCulture)).ToArray(); // XElement[] if (typeof(T) == typeof(XElement[])) return (T)(object)nodeList.OfType().ToArray(); // List if (typeof(T) == typeof(List)) return (T)(object)nodeList.OfType().ToList(); // object → return string[] for multiple text values if (typeof(T) == typeof(object)) return (T)(object)textValues; // Try T[] via element-wise parsing if (typeof(T).IsArray) { var elemType = typeof(T).GetElementType()!; var arr = Array.CreateInstance(elemType, textValues.Length); for (int i = 0; i < textValues.Length; i++) arr.SetValue(RouteDecimal.ChangeType(textValues[i], elemType, System.Globalization.CultureInfo.InvariantCulture), i); return (T)(object)arr; } throw new InvalidOperationException( $"Cannot convert {nodeList.Count} XPath nodes to {typeof(T).Name}."); } // ── Helpers ── /// /// Extracts a string value from an XPath node (XElement, XAttribute, XText, etc.). /// private string Text(object node) { var value = ExtractTextValue(node); return _trim ? value.Trim() : value; } /// /// Extracts a string value from an XPath node (XElement, XAttribute, XText, etc.). /// internal static string ExtractTextValue(object node) => node switch { XElement e => e.Value, XAttribute a => a.Value, XText t => t.Value, XComment c => c.Value, XProcessingInstruction pi => pi.Data, _ => node?.ToString() ?? string.Empty }; /// /// Tries to parse a text value to the best-fitting CLR type (int, double, bool, or string). /// Used for Evaluate<object>. /// private static object SmartParseText(string text) { if (int.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var i)) return i; if (double.TryParse(text, System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.AllowThousands, System.Globalization.CultureInfo.InvariantCulture, out var d)) return d; if (bool.TryParse(text, out var b)) return b; return text; } /// /// Parses a text value to a specific type. /// private static T ParseText(string text) { var targetType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); if (targetType == typeof(bool)) { // Support both "true"/"false" and "1"/"0" if (bool.TryParse(text, out var b)) return (T)(object)b; if (text == "1") return (T)(object)true; if (text == "0") return (T)(object)false; } try { return (T)RouteDecimal.ChangeType(text, targetType, System.Globalization.CultureInfo.InvariantCulture); } catch (Exception ex) { throw new InvalidOperationException( $"Cannot convert XPath text value '{text}' to {typeof(T).Name}.", ex); } } /// public override void SetValue(IExchange exchange, object value) { throw new NotSupportedException("XPathExpression does not support setting values."); } /// /// /// Thrown when the expression carries anything the template cannot: a non-default /// , a source, or bound parameters. The one-argument xpath() /// function of the expression language takes the path and nothing else, so a template string /// would quietly drop the rest and mean something different from the expression it was /// serialized from — a parameterised path would even fail at evaluation, its $name /// left unbound. Refusing loudly beats serialising a lie. /// public override string ToTemplateString() { if (_result != XPathResult.NodeSet) throw new NotSupportedException( $"XPathExpression with result type {_result} cannot be serialized to a template string: " + "the xpath() function takes only a path."); if (_source is not null) throw new NotSupportedException( "XPathExpression with a source cannot be serialized to a template string: " + "the template would silently read the body instead."); if (_parameters is { Count: > 0 }) throw new NotSupportedException( "XPathExpression with bound parameters cannot be serialized to a template string: " + "the bindings would be dropped, leaving the path's $variables unbound."); return $"${{xpath({_xpath})}}"; } }