PureXML/Sources/XSLT/XSLTRuntime.swift
Mihaela Mihaljevic ef4e7afa7a feat(xslt): post-order import precedence, scoped apply-imports, relative hrefs
Import precedences are assigned in post-order over the import tree, so
sibling imports are distinct and every unit sits above its own
imports; apply-imports searches only the current template's
stylesheet's own import subtree per 5.6, reverting to the built-in
rule when that unit imports nothing; an included stylesheet's
declarations join the including unit at its precedence while its
imports join the import list; and include/import hrefs resolve against
the including stylesheet's URI. Included documents' roots stay
retained until their declarations are parsed (tree parents are weak,
so the in-scope xmlns chain could vanish mid-compile). Xalan baseline
159 -> 144 (reluri 11 -> 3, impincl 9 -> 2).
2026-06-10 16:27:06 +02:00

62 lines
2.3 KiB
Swift

extension PureXML.XSLT {
/// One produced item: a node for the result tree, or an attribute to attach to
/// the enclosing element.
enum ResultItem {
case node(PureXML.Model.Node)
case attribute(PureXML.Model.Attribute)
}
/// The evaluation context during instantiation.
struct XSLTContext {
var node: PureXML.Model.TreeNode
/// The current node when it is not a tree node (an attribute or
/// namespace node); `node` then holds its owner element.
var current: PureXML.XPath.Node?
var position: Int
var size: Int
var variables: [String: PureXML.XPath.Value]
/// The XPath context node: `current` when set, else the tree node.
var focus: PureXML.XPath.Node {
current ?? .tree(node)
}
/// The mode of the template currently being instantiated, so `apply-imports`
/// re-applies in the same mode.
var mode: String?
/// The import precedence of the template currently being instantiated, so
/// `apply-imports` considers only templates below it. `.max` outside a template.
var importPrecedence: Int
/// The floor of the current template's stylesheet's import subtree;
/// apply-imports searches [importRangeLow, importPrecedence).
var importRangeLow: Int = 0
/// The stylesheet xmlns bindings of the template being instantiated,
/// so prefixed names in its expressions resolve by URI.
var namespaces: [String: String] = [:]
init(
node: PureXML.Model.TreeNode,
current: PureXML.XPath.Node? = nil,
position: Int,
size: Int,
variables: [String: PureXML.XPath.Value],
mode: String? = nil,
importPrecedence: Int = .max,
) {
self.node = node
self.current = current
self.position = position
self.size = size
self.variables = variables
self.mode = mode
self.importPrecedence = importPrecedence
}
}
/// Shared, mutable signal that an `xsl:message terminate="yes"` fired, carrying
/// its text. A reference type so the value-semantics transformer can set it.
final class Termination {
var message: String?
}
}