* fix(xslt): resolve string-form document() against the stylesheet element's base URI
A string-form document() reference resolves against the base URI of the
stylesheet element holding it (XSLT 1.0 12.1). For a declaration in an
included or imported file that is the included file's URI, not the top
stylesheet's, so document('data.xml') (or document('')) in an included file
must resolve against the included file.
The parser already tracked each top-level declaration's source-file base but
discarded it after collection. That base is now carried onto the compiled
templates and global variables, into the runtime context, and through to the
function table, so the document() string form resolves against it. Templates
and globals both carry it, so the rule holds wherever the call appears.
Closes Apache Xalan conformance cases mdocs12 and mdocs13.
* fix(xslt): carry base URI into for-each and sort contexts; keep document('') self-referential
Follow-up to the string-form document() base URI work. Three coverage gaps:
- xsl:for-each and xsl:sort evaluate their body and key expressions in a
context derived from, not equal to, the enclosing template's, so a
document() call there resolved against the empty base when the template came
from an included file. Both now inherit the enclosing context's base URI.
- A non-empty base URI made document('') in the top stylesheet resolve to that
base and route through the loader, rather than returning the stylesheet's own
parsed document as before. The parsed stylesheet is now registered under its
base URI, so document('') reaches it without the loader; document('') in an
included file (a different base) still loads that file.
43 lines
2.1 KiB
Swift
43 lines
2.1 KiB
Swift
extension PureXML.XSLT.Transformer {
|
|
/// A top-level variable or parameter declaration, extracted from the
|
|
/// stylesheet's globals for fixpoint evaluation.
|
|
private struct Global {
|
|
let name: String
|
|
let select: String?
|
|
let body: [PureXML.XSLT.Instruction]
|
|
let base: String
|
|
}
|
|
|
|
/// The global variable bindings, resolved to a fixpoint so references
|
|
/// between top-level variables and parameters hold in any document order
|
|
/// (XSLT 1.0 section 11.4). Evaluating once top-to-bottom would leave a
|
|
/// forward reference (a global declared before the one it uses) unresolved.
|
|
/// A DAG of N globals settles in at most N passes; a pass that changes
|
|
/// nothing stops early; a genuine circular reference stops at the cap with
|
|
/// the offending variables left at their last value.
|
|
func evaluatedGlobals() -> [String: PureXML.XPath.Value] {
|
|
var variables: [String: PureXML.XPath.Value] = [:]
|
|
let globals: [Global] = stylesheet.globals.compactMap {
|
|
guard case let .variable(name, select, body) = $0.instruction else { return nil }
|
|
return Global(name: name, select: select, body: body, base: $0.baseURI)
|
|
}
|
|
// A caller-supplied value overrides an xsl:param default and is fixed.
|
|
for global in globals where stylesheet.parameterNames.contains(global.name) {
|
|
if let supplied = parameters[global.name] { variables[global.name] = .string(supplied) }
|
|
}
|
|
for _ in 0 ..< max(1, globals.count) {
|
|
var changed = false
|
|
for global in globals where !(stylesheet.parameterNames.contains(global.name) && parameters[global.name] != nil) {
|
|
let context = XSLTContext(node: root, position: 1, size: 1, variables: variables, baseURI: global.base)
|
|
let value = variableValue(global.select, global.body, context)
|
|
if variables[global.name] != value {
|
|
variables[global.name] = value
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed { break }
|
|
}
|
|
return variables
|
|
}
|
|
}
|