Path: redb-route/redb.Route/Expressions/CompiledXPathExpression.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/CompiledXPathExpression.cs
----
using System;
using redb.Route.Abstractions;
namespace redb.Route.Expressions;
///
/// An XPath expression whose path is computed per message, delegating the evaluation itself
/// to .
///
///
///
/// The ordinary case is a fixed path over a varying document. This is the other one: a route that
/// decides which path to run from the message — a rule id in a header, a per-tenant selector in an
/// exchange property. It costs an XPath compilation per message, which is why it is a separate type
/// rather than the default behaviour.
///
///
/// Until 2026-09-03 it did not implement , so despite being public and
/// tested there was nowhere in the DSL to put it: every position that takes an expression takes
/// that interface. It now is one, which makes it usable in the value, condition, routing and stream
/// positions, and in the EIPs that accept an expression.
///
///
public class CompiledXPathExpression : Expression
{
private readonly Func _pathDelegate;
///
/// Initializes a new instance of the class.
///
/// A delegate that computes the XPath string from an .
public CompiledXPathExpression(Func pathDelegate)
{
_pathDelegate = pathDelegate ?? throw new ArgumentNullException(nameof(pathDelegate));
}
///
/// Evaluates the dynamic XPath expression against the exchange.
///
/// The type of the return value.
/// The exchange containing the message data.
///
/// The value extracted by the computed XPath, converted to , or the
/// default when the delegate produced no path.
///
public override T Evaluate(IExchange exchange)
{
var path = _pathDelegate(exchange)?.ToString();
if (string.IsNullOrEmpty(path))
return default(T);
var xpathExpression = new XPathExpression(path);
return xpathExpression.Evaluate(exchange);
}
///
public override void SetValue(IExchange exchange, object value)
=> throw new NotSupportedException("CompiledXPathExpression does not support setting values.");
///
///
/// Always thrown: the path exists only per message, so there is no fixed text to serialize.
///
public override string ToTemplateString()
=> throw new NotSupportedException(
"CompiledXPathExpression computes its path per message and has no template form.");
}