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
patternmatchnode? 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) forA|B|…. Sees through the syntheticBackwardsCompatwrapper the compiler adds in aversion="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
nodeundermode. XSLT 1.0 conflict resolution (§5.5): - select_
template_ max_ precedence - Variant of
select_templateused byxsl:apply-imports: limits candidates to templates whoseimport_precedenceis at mostmax_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 aftercurrent. Conflict order is (precedence descending, priority descending, source position descending — last in source wins ties); “next” means strictly less along that order thancurrent. ReturnsNonewhen 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:keyreferences in the matched patterns). UsesNoBindings. - 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.