Helium provides two distinct XPath implementations designed for different use cases and specification levels:
xpath1)xpath3)Both engines share a common underlying infrastructure for efficient and consistent DOM tree traversal and resource management, built within the internal/xpath package. They are tightly integrated with the Helium DOM (helium.Node) and follow a compile-once, evaluate-many pattern for optimized XPath expression evaluation.
internal/xpathAt the core of both XPath engines lies the internal/xpath package, which implements specification-agnostic functionality crucial for XPath evaluation. Key features include:
TraverseAxis internal/xpath/axes.go39DocOrderCache and methods like DeduplicateNodes to sort node sets by XML document order and remove duplicates, which is critical for correct XPath result semantics internal/xpath/docorder.go45-48DefaultMaxRecursionDepth and DefaultMaxNodeSetLength to guard against pathological expressions causing resource exhaustion internal/xpath/limits.go34This shared internal layer ensures consistency in fundamental behaviors across both XPath 1.0 and 3.1 implementations.
Sources: .claude/docs/xpath3-architecture.md27-34 .claude/docs/xpath3-architecture.md36-51
| Aspect | XPath 1.0 Engine (xpath1) | XPath 3.1 Engine (xpath3) |
|---|---|---|
| Specification | W3C XPath 1.0 | W3C XPath 3.1 |
| Data Model | Node-sets, Strings, Numbers, Booleans | Strongly-typed Sequences of Items (Nodes, Atomics, Maps, Arrays, Functions) |
| Execution Model | Direct AST interpretation and evaluation | VM-based bytecode execution with a compiler fast-path fallback |
| Type System | Weak and implicit (simple unions) | Rich, XSD-aware, strict types with atomic types and sequences |
| Extensibility | Custom Go functions via FunctionContext | Higher-Order Functions, inline functions, and custom Go functions |
| Performance | Direct evaluation for legacy scenarios | Compiled to VM instructions with optimizations for common patterns |
Sources: .claude/docs/xpath3-architecture.md6-9 .claude/docs/xpath3-architecture.md63-86 .claude/docs/xpath3-design.md23-26
Below is a representation of the XPath 3.1 expression evaluation pipeline and its relationship with shared internal infrastructure components. XPath 1.0 has a simpler but analogous pipeline using xpath1.
This pipeline tokenizes the input, performs parsing to form an AST, applies a fast-path compiler for simple expressions xpath3/compile_direct.go75 or lowers to a full VM bytecode program xpath3/compiler.go76 The VM then executes expressions against a context node using evalContext state xpath3/eval_state.go86 accessing shared axis traversal and ordering logic.
Sources: .claude/docs/xpath3-architecture.md20-25 .claude/docs/xpath3-architecture.md134-137
Helium uses the Context Carrier Pattern for configuring XPath evaluation parameters. The system uses functions that mutate Go's context.Context to set up evaluation environments.
Common configuration mutators supported by both engines include:
WithNamespaces(ctx, nsMap) xpath1/xpath.go54 xpath3/xpath3.go73 — adds prefix-to-URI mappings for QName resolution.WithVariables(ctx, varsMap) xpath1/xpath.go55 xpath3/xpath3.go74 — binds variable names to values.WithFunctions(ctx, funcsMap) xpath1/xpath.go58 xpath3/xpath3.go76 — registers custom extension functions.WithURIResolver(ctx, resolver) xpath3/xpath3.go78 — (XPath 3.1) configures resolution for external resources like fn:doc.Sources: .claude/docs/xpath3-architecture.md110-112 .claude/docs/xpath3-architecture.md95-96 .claude/docs/packages.md54-56
xpath1)Choose xpath1 if:
FunctionContext xpath1/xpath.go58For details, see XPath 1.0 Engine (xpath1).
xpath3)Choose xpath3 if:
fn:, math:, map:, array:) xpath3/function_library.go110XPathError xpath3/errors.go131For details, see XPath 3.1 Engine — Public API (xpath3).
Sources: .claude/docs/xpath3-architecture.md6-9 .claude/docs/xpath3-design.md9-16
xpath1 package: Find/Evaluate APIs, lexer/parser/evaluator pipeline, and custom function state.xpath3 public API: NewCompiler, Compile, Expression.Evaluate, Result, and context configuration.vmProgram structure, and resource limits.Item hierarchy, Sequence as eager []Item, atomization, and EBV logic.Sources: .claude/docs/xpath3-architecture.md6-9 .claude/docs/xpath3-design.md1-40
Refresh this wiki
This wiki was recently refreshed. Please wait 1 day to refresh again.