Path: redb-route/redb.Route/Expressions/XPathVariableContext.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/XPathVariableContext.cs ---- using System; using System.Collections.Generic; using System.Globalization; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; using redb.Route.Abstractions; namespace redb.Route.Expressions; /// /// Supplies bound values to the $name variables of an XPath expression. /// /// /// /// The BCL resolves XPath variables only through an , which it accepts in /// the same slot as a plain namespace resolver — so one object carries both the prefixes and the /// values. derives from , which is what /// makes carrying both natural rather than a trick. /// /// /// Built per evaluation, because the values come from the message. The path is not rebuilt, which /// is the whole point: the same query text runs whatever the data. /// /// internal sealed class XPathVariableContext : XsltContext { private readonly Dictionary _values = new(StringComparer.Ordinal); private XPathVariableContext() : base(new NameTable()) { } internal static XPathVariableContext Build( IXmlNamespaceResolver? namespaces, IReadOnlyList<(string Name, IExpression Value)> parameters, IExchange exchange) { var context = new XPathVariableContext(); if (namespaces is not null) { foreach (var (prefix, uri) in namespaces.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml)) context.AddNamespace(prefix, uri); } foreach (var (name, value) in parameters) context._values[name] = ToXPathValue(value.Evaluate(exchange)); return context; } /// /// Converts a CLR value to one of the four types XPath 1.0 knows. /// /// /// XPath has no integers, no dates and no null: numbers are doubles, and everything it cannot /// place is a string. Converting here rather than at the call site keeps the coercion in one /// place, and keeps it invariant — a decimal separator that follows the server's locale would /// make the same route match different documents on different machines. /// private static object ToXPathValue(object? value) => value switch { null => string.Empty, bool b => b, string s => s, double d => d, float f => (double)f, decimal m => (double)m, sbyte or byte or short or ushort or int or uint or long or ulong => Convert.ToDouble(value, CultureInfo.InvariantCulture), IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture), _ => value.ToString() ?? string.Empty }; /// /// /// Only unprefixed variables resolve. WithParameters binds local names alone — it /// rejects even a leading $ — so $x:id can never have a binding here, and /// matching it to the binding named id would answer a different question than the one /// the path asked. Returning null lets the engine fail with its own "variable not /// defined", which is the honest outcome. /// public override IXsltContextVariable? ResolveVariable(string prefix, string name) => string.IsNullOrEmpty(prefix) && _values.TryGetValue(name, out var value) ? new BoundValue(value) : null; /// public override IXsltContextFunction? ResolveFunction(string prefix, string name, XPathResultType[] argTypes) => null; /// public override bool Whitespace => true; /// public override bool PreserveWhitespace(XPathNavigator node) => true; /// public override int CompareDocument(string baseUri, string nextbaseUri) => 0; /// One bound value, presented to the engine with the XPath type it actually has. private sealed class BoundValue(object value) : IXsltContextVariable { public bool IsLocal => false; public bool IsParam => false; public XPathResultType VariableType => value switch { double => XPathResultType.Number, bool => XPathResultType.Boolean, _ => XPathResultType.String }; public object Evaluate(XsltContext xsltContext) => value; } }