Path: redb-route/redb.Route.XPath2/XPath2Values.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/XPath2Values.cs
----
using System.Globalization;
using System.Xml.Linq;
using System.Xml.XPath;
using redb.Route.Predicates;
using EngineIterator = Wmhelp.XPath2.XPath2NodeIterator;
namespace redb.Route.XPath2;
///
/// Turns what the XPath 2.0 engine returns into what a route asked for.
///
///
/// XPath 2.0 returns a sequence where 1.0 returned a node-set, and a sequence can hold atomic
/// values as well as nodes — tokenize('a,b', ',') yields two strings that were never in the
/// document. The conversions below therefore work on items rather than on nodes, but land on the
/// same shapes the XPath 1.0 expression produces, so a route can move between the two languages
/// without its surrounding code changing.
///
internal static class XPath2Values
{
///
/// Reads a result in a condition position.
///
///
/// A sequence answers whether anything matched, exactly as a node-set does in XPath 1.0. A
/// single atomic value is a value like any other and goes through the one DSL truthiness rule,
/// so "0" is false there — which is the rule this framework applies everywhere, and is
/// deliberately not XPath 2.0's effective-boolean-value table, whose answer for a lone
/// "0" string is true.
///
internal static bool ToBoolean(object? raw)
{
if (raw is not EngineIterator iterator)
return RouteTruthiness.ToBoolean(raw);
var items = Drain(iterator);
return items.Count switch
{
0 => false,
1 when items[0] is not XObject => RouteTruthiness.ToBoolean(items[0]),
_ => true
};
}
/// Converts a result to the requested CLR type.
internal static T Convert(object? raw, string path, bool trim)
{
if (raw is not EngineIterator iterator)
return ConvertScalar(raw, path, trim);
var items = Drain(iterator);
if (items.Count == 0)
{
if (typeof(T).IsValueType && Nullable.GetUnderlyingType(typeof(T)) is null)
throw new InvalidOperationException(
$"No values found for XPath 2.0 expression '{path}' (expected non-nullable {typeof(T).Name}).");
return default!;
}
return items.Count == 1
? ConvertSingle(items[0], path, trim)
: ConvertMany(items, path, trim);
}
///
/// Converts a CLR value into something the engine will bind to a $name variable.
///
///
/// Formatting is invariant for the same reason it is everywhere else here: a decimal separator
/// taken from the server's locale would make one route match different documents on different
/// machines.
///
internal static object ToEngineValue(object? value) => value switch
{
null => string.Empty,
bool or string or int or long or double or DateTime => value,
decimal m => (double)m,
float f => (double)f,
sbyte or byte or short or ushort or uint or ulong
=> System.Convert.ToInt64(value, CultureInfo.InvariantCulture),
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
_ => value.ToString() ?? string.Empty
};
// ── Items ──
///
/// Reads a sequence into ordinary CLR objects: LINQ-to-XML nodes stay nodes, atomic values
/// become their typed values.
///
private static List