Skip to main content

Module pattern

Module pattern 

Source
Expand description

XSLT pattern matching — XSLT 1.0 §5.2.

XSLT patterns (match=, xsl:key match=, xsl:number count=/from=) are a restricted XPath grammar. Rather than implement a separate matcher, we compile patterns as full XPath expressions and use the trick from the spec:

A node matches a pattern if there is some possible context such that evaluating the pattern as an expression with that context yields a node-set containing the node being matched.

Concretely: walk the ancestor-or-self chain of the candidate node, evaluating the pattern as an XPath at each one. If any evaluation produces a node-set containing the candidate, the pattern matches.

For default priority we look at the pattern’s shape — a name test prefix:local defaults to 0, * to -0.5, etc., per §5.5. A | union behaves as several template rules, one per alternative (XSLT 2.0 §6.4): template selection flattens the union via pattern_branches and evaluates each branch as its own logical rule, with its own default priority. xsl:next-match can therefore step from one branch to another within the same template before falling through to a different template.

Template selection picks the best template per XSLT’s priority + import-precedence rules. Currently a linear scan across all templates; an index by innermost-step-name is a known optimisation tracked for later.

Structs§

Selected
Result of looking up a template: a borrow into the stylesheet’s template list plus the effective priority used to break ties.

Functions§

default_priority
Compute the default priority for a pattern AST node. The XSLT spec defines four buckets:
matches
Does pattern match node? Walks the ancestor-or-self chain and evaluates the pattern at each context until either a match is found or the chain is exhausted.
pattern_branches
Flatten a pattern’s outermost union into its operands. Returns [pat] for non-union patterns, [A, B, …] (in source order) for A|B|…. Sees through the synthetic BackwardsCompat wrapper the compiler adds in a version="1.0" scope, but does not look inside nested expressions (only the union shape at the top matters for §6.4 splitting).
select_template
Find the best-matching template for node under mode. XSLT 1.0 conflict resolution (§5.5):
select_template_max_precedence
Variant of select_template used by xsl:apply-imports: limits candidates to templates whose import_precedence is at most max_precedence. Conflict resolution within that pool follows the same rules.
select_template_next
XSLT 2.0 §6.7 xsl:next-match — pick the next template in the conflict-resolution order after current. Conflict order is (precedence descending, priority descending, source position descending — last in source wins ties); “next” means strictly less along that order than current. Returns None when no such template matches the node + mode.
select_template_no_bindings
Convenience entry point for callers that don’t need to thread custom XPath bindings (e.g. simple stylesheets with no xsl:key references in the matched patterns). Uses NoBindings.
set_on_multiple_match_error
Set whether unresolved template conflicts are reported (XTRE0540) instead of recovered. Returns the previous value so the caller can restore it after the apply.