Path: redb-route/redb.Route.XPath2/XPath2Dsl.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.XPath2/XPath2Dsl.cs
----
using System.Xml;
using redb.Route.Abstractions;
namespace redb.Route.XPath2;
///
/// Factory methods for XPath 2.0 expressions, written to be imported:
/// using static redb.Route.XPath2.XPath2Dsl;
///
///
///
/// The XPath 1.0 helpers live on RouteBuilder as protected members, which a package outside
/// the core assembly cannot extend. A static import gives the same shape at the call site —
/// .Filter(XPath2("some $o in /orders/order satisfies $o/total > 100")) — without the core
/// having to know this package exists.
///
///
public static class XPath2Dsl
{
/// Creates an XPath 2.0 expression over the message body.
/// The XPath 2.0 expression text.
public static XPath2Expression XPath2(string path) => new(path);
/// Creates an XPath 2.0 expression with namespace prefixes bound.
/// The XPath 2.0 expression text.
/// Prefix/URI pairs.
///
/// The prefixes go in here rather than into a fluent call, because XPath 2.0 resolves them
/// while compiling and the expression compiles as soon as it exists — see
/// .
///
public static XPath2Expression XPath2(string path, params (string Prefix, string Uri)[] namespaces)
=> new(path, XPath2Expression.Namespaces(namespaces));
/// Creates an XPath 2.0 expression with an explicit namespace resolver.
/// The XPath 2.0 expression text.
/// The resolver to use.
public static XPath2Expression XPath2(string path, IXmlNamespaceResolver namespaces) => new(path, namespaces);
/// Creates an XPath 2.0 expression whose result is converted to .
/// The XPath 2.0 expression text.
public static TypedXPath2Expression XPath2(string path) => new(path);
///
/// Java-style alias, matching the xpath(...) spelling of the 1.0 helpers.
public static XPath2Expression xpath2(string path) => new(path);
///
/// Java-style alias.
public static TypedXPath2Expression xpath2(string path) => new(path);
}
///
/// An XPath 2.0 expression that remembers the CLR type its result should take, so a pipeline
/// asking for Evaluate<object> still gets that type.
///
/// The target CLR type.
public sealed class TypedXPath2Expression : redb.Route.Expressions.Expression
{
private readonly XPath2Expression _inner;
/// Creates a typed XPath 2.0 expression.
/// The XPath 2.0 expression text.
public TypedXPath2Expression(string path) : this(new XPath2Expression(path))
{
}
private TypedXPath2Expression(XPath2Expression inner) => _inner = inner;
///
public TypedXPath2Expression From(IExpression source) => new(_inner.From(source));
///
public TypedXPath2Expression Trimmed(bool trim = true) => new(_inner.Trimmed(trim));
///
public TypedXPath2Expression WithParameters(params (string Name, IExpression Value)[] parameters)
=> new(_inner.WithParameters(parameters));
///
public override T Evaluate(IExchange exchange)
{
if (typeof(T) == typeof(object))
return (T)(object)_inner.Evaluate(exchange)!;
return _inner.Evaluate(exchange);
}
///
public override void SetValue(IExchange exchange, object value) => _inner.SetValue(exchange, value);
}