PureXML/Sources/XPath/EvaluationContext.swift
Mihaela Mihaljevic 2a77b56d2f feat(xpath): eval-time namespace context and XPointer xmlns()/xpath1() schemes
- Eval-time prefix bindings: Query.evaluate(over:namespaces:) and
  value(at:...:namespaces:) supply prefix to URI bindings, so a name test
  like q:item resolves q to a URI and matches by namespace regardless of
  the document's own prefix. The match is namespace-aware only when
  bindings are present, so existing unprefixed/prefix-string queries are
  unchanged.
- XPointer schemes: xpath1() evaluates an XPath expression (like
  xpointer()), and xmlns(p=uri) binds a prefix for the expression parts
  that follow it, threaded into the XPath evaluation.

The range() scheme (and so XInclude range inclusion) remains.

Part of #88
2026-06-08 16:00:15 +02:00

65 lines
2.7 KiB
Swift

extension PureXML.XPath {
/// One XPath function: it receives its already-evaluated argument values and
/// the evaluation context, and returns a value.
typealias FunctionImplementation = (_ arguments: [Value], _ context: EvaluationContext) throws -> Value
/// The evaluation context an expression is evaluated against: the context
/// node, its position and size within the current node-set, the variable
/// bindings, and the function library. Predicates and the `position()` and
/// `last()` functions read the position and size.
struct EvaluationContext {
var node: Node
var position: Int
var size: Int
var variables: [String: Value]
var functions: FunctionTable
/// Prefix-to-URI bindings supplied at evaluation time, so a name test like
/// `x:foo` resolves `x` to a URI and matches by namespace rather than by the
/// document's own prefix. Empty by default, in which case matching falls
/// back to the in-document prefix string.
var namespaces: [String: String] = [:]
/// A copy positioned on `node` at one-based `position` within a node-set of
/// `size`, keeping the same variables, functions, and namespace bindings.
func focused(on node: Node, position: Int, size: Int) -> EvaluationContext {
EvaluationContext(
node: node,
position: position,
size: size,
variables: variables,
functions: functions,
namespaces: namespaces,
)
}
}
/// The XPath function library: a name-to-implementation table. The core set is
/// built in; further functions extend it.
struct FunctionTable {
private var table: [String: FunctionImplementation]
init(_ table: [String: FunctionImplementation] = [:]) {
self.table = table
}
/// Returns a table with `implementation` registered under `name`.
func adding(_ name: String, _ implementation: @escaping FunctionImplementation) -> FunctionTable {
var copy = table
copy[name] = implementation
return FunctionTable(copy)
}
/// Merges another table's entries over this one's.
func merging(_ other: FunctionTable) -> FunctionTable {
FunctionTable(table.merging(other.table) { _, new in new })
}
func call(_ name: String, _ arguments: [Value], _ context: EvaluationContext) throws -> Value {
guard let implementation = table[name] else {
throw QueryError.unknownFunction(name)
}
return try implementation(arguments, context)
}
}
}