feat(xslt): stylesheet namespace resolution for patterns and expressions
Templates carry their in-scope xmlns bindings into match patterns (match cache keyed on pattern plus bindings) and instantiated expressions, resolving prefixed name tests by namespace URI; with bindings supplied an unprefixed test selects only the null namespace, the XPath 1.0 rule. Instruction-scope capture includes the default declaration: xsl:element honors in-scope xmlns="uri" for unprefixed names, xmlns="" keeps them namespace-free, and an unusable created name drops the wrapper, keeping content. Xalan baseline 263 -> 218 (namespace 41 -> 13).
This commit is contained in:
parent
da8bb6b92a
commit
1646128b35
@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- Stylesheet namespace resolution, eighth xalan burn-down (#130): a template carries the xmlns bindings in scope at its stylesheet element, and they flow into its match pattern (the match cache keys on pattern plus bindings) and every expression evaluated while it is instantiated, so `match="baz2:doc"` and `select="local-name(baz2:b)"` resolve the prefix against the stylesheet and match by namespace URI; with bindings supplied the XPath 1.0 rule applies exactly, an unprefixed name test selecting only the null namespace. The instruction-scope namespace capture includes the default declaration, so `xsl:element` honors an in-scope `xmlns="uri"` for its unprefixed created names, an in-scope `xmlns=""` keeps them namespace-free (undeclared in the result when an inherited default would capture them), and an unusable created name (`:foo`) drops the wrapper and keeps the content, the Xalan recovery. Baseline 263 to 218 (namespace 41 to 13, axes 16, sort 18, output unchanged at 34, the rest spread).
|
||||
- Keys, ids, and doctyped sources, seventh xalan burn-down (#130): `key()` and `document()` read the string value of attribute and namespace node arguments instead of dropping them (the Muenchian grouping idiom works); match patterns evaluate with the XSLT function table, so `key('k', 'v')` and `id('x')` patterns match (a key/id pattern branch is also no longer prefixed with `//`); and the XSLT entry points parse sources, stylesheets, and `document()` loads with doctypes allowed, which the conformance corpora use throughout. Baseline 292 to 263 (idkey 39 to 16, the rest spread).
|
||||
- Templates over attribute and namespace nodes, sixth xalan burn-down (#130): xsl:apply-templates and xsl:for-each now keep attribute and namespace nodes in their selections instead of dropping them at the tree-node conversion. The evaluation context carries the non-tree current node (its owner element backs the tree-only machinery), XPath evaluation and current() start from it, match patterns select attribute nodes (the match cache keeps attribute identities, owner plus name, alongside tree identities), the built-in rules copy an attribute's or namespace's value as text, xsl:copy copies the focused attribute or namespace declaration itself, and an attribute result arriving after element content is ignored (the XSLT recovery). Sorting and apply-imports work over the generalized node kind. Baseline 352 to 292 (axes 35 to 16, copy 29 to 16, attribset 18 to 13, the rest spread).
|
||||
- Output methods in depth, fifth xalan burn-down (#130): `disable-output-escaping` on xsl:value-of and xsl:text (raw text travels bracketed by private-use sentinels and the serializer's escaping is undone after the fact; a string extracted from a result tree fragment loses the markers per 16.4, so the variable round-trip case re-escapes); the default output method is html when the first result element is named html with a null namespace (16.1); the xml method writes the XML declaration unless `omit-xml-declaration="yes"` (the spec default, with the caller's emit options as fallback only on explicit stylesheet silence); the html method injects `META http-equiv="Content-Type"` into head (16.2), keeps SCRIPT/STYLE raw and void elements end-tag-free case-insensitively, and escapes U+00A0-U+00FF to the HTML 4.01 Latin-1 entity names. Whitespace-only detection in stylesheet/source stripping now means XML whitespace only, so an NBSP-only text node survives. The conf comparison treats whitespace adjacent to tag boundaries as layout for non-canonical comparisons. Baseline 371 to 352 (output 49 to 30).
|
||||
|
||||
@ -99,11 +99,17 @@ extension PureXML.XPath.Evaluator {
|
||||
/// local name; otherwise it falls back to the in-document prefix string (the
|
||||
/// behavior when no bindings are supplied).
|
||||
private static func qualifiedMatches(_ qualified: PureXML.Model.QualifiedName, _ name: String, _ namespaces: [String: String]) -> Bool {
|
||||
if !namespaces.isEmpty, let colon = name.firstIndex(of: ":") {
|
||||
let prefix = String(name[..<colon])
|
||||
if let uri = namespaces[prefix] {
|
||||
let local = String(name[name.index(after: colon)...])
|
||||
return qualified.localName == local && (qualified.namespaceURI ?? "") == uri
|
||||
if !namespaces.isEmpty {
|
||||
if let colon = name.firstIndex(of: ":") {
|
||||
let prefix = String(name[..<colon])
|
||||
if let uri = namespaces[prefix] {
|
||||
let local = String(name[name.index(after: colon)...])
|
||||
return qualified.localName == local && (qualified.namespaceURI ?? "") == uri
|
||||
}
|
||||
} else {
|
||||
// With bindings supplied, the XPath 1.0 rule applies exactly:
|
||||
// an unprefixed name test selects the null namespace.
|
||||
return qualified.localName == name && (qualified.namespaceURI ?? "").isEmpty
|
||||
}
|
||||
}
|
||||
return qualified.description == name || qualified.localName == name
|
||||
|
||||
@ -95,6 +95,7 @@ public extension PureXML.XPath {
|
||||
size: Int,
|
||||
variables: [String: Value],
|
||||
functions: FunctionTable,
|
||||
namespaces: [String: String] = [:],
|
||||
) throws -> Value {
|
||||
try Evaluator.value(
|
||||
expression,
|
||||
@ -103,6 +104,7 @@ public extension PureXML.XPath {
|
||||
size: size,
|
||||
variables: variables,
|
||||
functions: functions,
|
||||
namespaces: namespaces,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -61,7 +61,7 @@ extension PureXML.XSLT.Transformer {
|
||||
} else if let uri = namespaces[prefix] {
|
||||
name = PureXML.Model.QualifiedName(prefix: prefix, localName: name.localName, namespaceURI: uri)
|
||||
}
|
||||
} else if !isAttribute, let defaultURI = namespaces[""] {
|
||||
} else if !isAttribute, let defaultURI = namespaces[""], !defaultURI.isEmpty {
|
||||
name = PureXML.Model.QualifiedName(prefix: nil, localName: name.localName, namespaceURI: defaultURI)
|
||||
}
|
||||
return name
|
||||
@ -71,6 +71,13 @@ extension PureXML.XSLT.Transformer {
|
||||
guard case let .element(nameTemplate, namespaceTemplate, namespaces, useAttributeSets, body) = instruction else {
|
||||
return []
|
||||
}
|
||||
let raw = avt(nameTemplate, context)
|
||||
let parts = raw.split(separator: ":", omittingEmptySubsequences: false)
|
||||
if raw.isEmpty || parts.contains(where: \.isEmpty) || parts.count > 2 {
|
||||
// An unusable element name: the recovery is to emit the content
|
||||
// without the wrapper element.
|
||||
return instantiate(body, context).filter { if case .attribute = $0 { false } else { true } }
|
||||
}
|
||||
let name = createdName(nameTemplate, namespaceTemplate, namespaces, context, isAttribute: false)
|
||||
return [buildElement(name: name, literalAttributes: [], useAttributeSets: useAttributeSets, body: body, context)]
|
||||
}
|
||||
|
||||
@ -19,26 +19,37 @@ extension PureXML.XSLT {
|
||||
private var matched: [String: PatternMatches] = [:]
|
||||
|
||||
/// The identities of the tree nodes `pattern` selects over `root`.
|
||||
func nodes(matching pattern: String, over root: PureXML.Model.TreeNode, functions: PureXML.XPath.FunctionTable = .init()) -> Set<ObjectIdentifier> {
|
||||
compute(pattern, root, functions).trees
|
||||
func nodes(
|
||||
matching pattern: String,
|
||||
over root: PureXML.Model.TreeNode,
|
||||
functions: PureXML.XPath.FunctionTable = .init(),
|
||||
namespaces: [String: String] = [:],
|
||||
) -> Set<ObjectIdentifier> {
|
||||
compute(pattern, root, functions, namespaces).trees
|
||||
}
|
||||
|
||||
/// The identities of the attribute nodes `pattern` selects over `root`.
|
||||
func attributes(matching pattern: String, over root: PureXML.Model.TreeNode, functions: PureXML.XPath.FunctionTable = .init()) -> Set<AttributeIdentity> {
|
||||
compute(pattern, root, functions).attributes
|
||||
func attributes(
|
||||
matching pattern: String,
|
||||
over root: PureXML.Model.TreeNode,
|
||||
functions: PureXML.XPath.FunctionTable = .init(),
|
||||
namespaces: [String: String] = [:],
|
||||
) -> Set<AttributeIdentity> {
|
||||
compute(pattern, root, functions, namespaces).attributes
|
||||
}
|
||||
|
||||
/// Computed and cached on first use; a branch that fails to compile
|
||||
/// selects nothing. The function table makes `key()` and `id()`
|
||||
/// patterns work.
|
||||
private func compute(_ pattern: String, _ root: PureXML.Model.TreeNode, _ functions: PureXML.XPath.FunctionTable) -> PatternMatches {
|
||||
if let cached = matched[pattern] { return cached }
|
||||
/// patterns work; the bindings resolve prefixed name tests by URI.
|
||||
private func compute(_ pattern: String, _ root: PureXML.Model.TreeNode, _ functions: PureXML.XPath.FunctionTable, _ namespaces: [String: String]) -> PatternMatches {
|
||||
let key = pattern + "\u{1}" + namespaces.sorted(by: { $0.key < $1.key }).map { $0.key + "=" + $0.value }.joined(separator: ";")
|
||||
if let cached = matched[key] { return cached }
|
||||
var result = PatternMatches()
|
||||
for branch in pattern.split(separator: "|") {
|
||||
let trimmed = branch.trimmingXMLWhitespace()
|
||||
let path = trimmed.hasPrefix("/") || trimmed.hasPrefix("key(") || trimmed.hasPrefix("id(") ? trimmed : "//" + trimmed
|
||||
guard let query = try? PureXML.XPath.Query(path),
|
||||
let value = try? query.value(atNode: .tree(root), position: 1, size: 1, variables: [:], functions: functions),
|
||||
let value = try? query.value(atNode: .tree(root), position: 1, size: 1, variables: [:], functions: functions, namespaces: namespaces),
|
||||
let selected = value.nodes
|
||||
else { continue }
|
||||
for node in selected {
|
||||
@ -52,7 +63,7 @@ extension PureXML.XSLT {
|
||||
}
|
||||
}
|
||||
}
|
||||
matched[pattern] = result
|
||||
matched[key] = result
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,6 +231,9 @@ public extension PureXML.XSLT {
|
||||
public var importPrecedence: Int
|
||||
public var parameters: [Binding]
|
||||
public var body: [Instruction]
|
||||
/// The stylesheet xmlns bindings in scope at the template, for
|
||||
/// resolving prefixes in its match pattern and expressions.
|
||||
public var namespaces: [String: String] = [:]
|
||||
}
|
||||
|
||||
/// A compiled stylesheet: its template rules, global variables, keys, output
|
||||
|
||||
@ -197,6 +197,7 @@ extension PureXML.XSLT {
|
||||
importPrecedence: precedence,
|
||||
parameters: parameters,
|
||||
body: body,
|
||||
namespaces: inScopeNamespaces(node).filter { !$0.key.isEmpty },
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -48,9 +48,11 @@ extension PureXML.XSLT.XSLTParser {
|
||||
var bindings: [String: String] = [:]
|
||||
var current: XSLTTree? = node
|
||||
while let candidate = current {
|
||||
for attribute in candidate.attributes where attribute.name.prefix == "xmlns" {
|
||||
if bindings[attribute.name.localName] == nil {
|
||||
for attribute in candidate.attributes {
|
||||
if attribute.name.prefix == "xmlns", bindings[attribute.name.localName] == nil {
|
||||
bindings[attribute.name.localName] = attribute.value
|
||||
} else if attribute.name.prefix == nil, attribute.name.localName == "xmlns", bindings[""] == nil {
|
||||
bindings[""] = attribute.value
|
||||
}
|
||||
}
|
||||
current = candidate.parent
|
||||
@ -64,12 +66,6 @@ extension PureXML.XSLT.XSLTParser {
|
||||
/// and `extension-element-prefixes` in scope.
|
||||
static func copiedNamespaces(_ node: XSLTTree) -> [String: String] {
|
||||
var bindings = inScopeNamespaces(node)
|
||||
var current: XSLTTree? = node
|
||||
while let candidate = current {
|
||||
let defaults = candidate.attributes.filter { $0.name.prefix == nil && $0.name.localName == "xmlns" }
|
||||
if bindings[""] == nil, let declared = defaults.first { bindings[""] = declared.value }
|
||||
current = candidate.parent
|
||||
}
|
||||
var excludedURIs: Set<String> = [XSLTNode.namespace]
|
||||
for prefix in excludedPrefixTokens(node) {
|
||||
let key = prefix == "#default" ? "" : prefix
|
||||
|
||||
@ -27,6 +27,9 @@ extension PureXML.XSLT {
|
||||
/// 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 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,
|
||||
|
||||
@ -45,7 +45,10 @@ extension PureXML.XSLT {
|
||||
|
||||
fileprivate func bestTemplate(for node: PureXML.XPath.Node, mode: String?, below ceiling: Int = .max) -> Template? {
|
||||
stylesheet.templates.enumerated()
|
||||
.filter { $0.element.mode == mode && $0.element.importPrecedence < ceiling && ($0.element.match.map { matches(node, $0) } ?? false) }
|
||||
.filter { entry in
|
||||
entry.element.mode == mode && entry.element.importPrecedence < ceiling
|
||||
&& (entry.element.match.map { matches(node, $0, entry.element.namespaces) } ?? false)
|
||||
}
|
||||
.max { lhs, rhs in
|
||||
(lhs.element.importPrecedence, lhs.element.priority, lhs.offset)
|
||||
< (rhs.element.importPrecedence, rhs.element.priority, rhs.offset)
|
||||
@ -53,17 +56,17 @@ extension PureXML.XSLT {
|
||||
.element
|
||||
}
|
||||
|
||||
func matches(_ node: PureXML.Model.TreeNode, _ pattern: String) -> Bool {
|
||||
matchCache.nodes(matching: pattern, over: root, functions: patternFunctions()).contains(ObjectIdentifier(node))
|
||||
func matches(_ node: PureXML.Model.TreeNode, _ pattern: String, _ namespaces: [String: String] = [:]) -> Bool {
|
||||
matchCache.nodes(matching: pattern, over: root, functions: patternFunctions(), namespaces: namespaces).contains(ObjectIdentifier(node))
|
||||
}
|
||||
|
||||
/// Pattern membership for any XPath node kind.
|
||||
func matches(_ node: PureXML.XPath.Node, _ pattern: String) -> Bool {
|
||||
func matches(_ node: PureXML.XPath.Node, _ pattern: String, _ namespaces: [String: String] = [:]) -> Bool {
|
||||
switch node {
|
||||
case let .tree(tree):
|
||||
matches(tree, pattern)
|
||||
matches(tree, pattern, namespaces)
|
||||
case let .attribute(owner, attribute):
|
||||
matchCache.attributes(matching: pattern, over: root, functions: patternFunctions())
|
||||
matchCache.attributes(matching: pattern, over: root, functions: patternFunctions(), namespaces: namespaces)
|
||||
.contains(PureXML.XSLT.AttributeIdentity(owner: ObjectIdentifier(owner), name: attribute.name.description))
|
||||
case .namespace:
|
||||
false
|
||||
@ -118,6 +121,7 @@ extension PureXML.XSLT {
|
||||
) -> [ResultItem] {
|
||||
var context = context
|
||||
context.importPrecedence = template.importPrecedence
|
||||
context.namespaces = template.namespaces
|
||||
for parameter in template.parameters {
|
||||
if let passed = parameters.first(where: { $0.name == parameter.name }) {
|
||||
context.variables[parameter.name] = variableValue(passed.select, passed.body, caller)
|
||||
@ -184,6 +188,7 @@ extension PureXML.XSLT {
|
||||
decimalFormats: stylesheet.decimalFormats,
|
||||
documents: documentCache,
|
||||
),
|
||||
namespaces: context.namespaces,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -49,10 +49,7 @@ copy/copy22
|
||||
copy/copy27
|
||||
copy/copy31
|
||||
copy/copy36
|
||||
copy/copy44
|
||||
copy/copy47
|
||||
copy/copy48
|
||||
copy/copy51
|
||||
copy/copy56
|
||||
copy/copy58
|
||||
copy/copy60
|
||||
@ -86,7 +83,6 @@ impincl/impincl23
|
||||
impincl/impincl25
|
||||
impincl/impincl26
|
||||
impincl/impincl27
|
||||
lre/lre12
|
||||
match/match14
|
||||
mdocs/mdocs01
|
||||
mdocs/mdocs05
|
||||
@ -110,50 +106,19 @@ namedtemplate/namedtemplate16
|
||||
namedtemplate/namedtemplate17
|
||||
namedtemplate/namedtemplate18
|
||||
namedtemplate/namedtemplate19
|
||||
namespace/namespace07
|
||||
namespace/namespace09
|
||||
namespace/namespace10
|
||||
namespace/namespace101
|
||||
namespace/namespace103
|
||||
namespace/namespace106
|
||||
namespace/namespace11
|
||||
namespace/namespace112
|
||||
namespace/namespace113
|
||||
namespace/namespace12
|
||||
namespace/namespace125
|
||||
namespace/namespace126
|
||||
namespace/namespace127
|
||||
namespace/namespace128
|
||||
namespace/namespace13
|
||||
namespace/namespace130
|
||||
namespace/namespace131
|
||||
namespace/namespace139
|
||||
namespace/namespace142
|
||||
namespace/namespace19
|
||||
namespace/namespace20
|
||||
namespace/namespace21
|
||||
namespace/namespace22
|
||||
namespace/namespace23
|
||||
namespace/namespace24
|
||||
namespace/namespace26
|
||||
namespace/namespace27
|
||||
namespace/namespace32
|
||||
namespace/namespace34
|
||||
namespace/namespace35
|
||||
namespace/namespace37
|
||||
namespace/namespace40
|
||||
namespace/namespace42
|
||||
namespace/namespace43
|
||||
namespace/namespace48
|
||||
namespace/namespace57
|
||||
namespace/namespace60
|
||||
namespace/namespace68
|
||||
namespace/namespace73
|
||||
namespace/namespace75
|
||||
namespace/namespace79
|
||||
node/node04
|
||||
node/node05
|
||||
node/node06
|
||||
node/node09
|
||||
node/node10
|
||||
node/node11
|
||||
@ -161,9 +126,6 @@ node/node12
|
||||
node/node13
|
||||
node/node15
|
||||
numbering/numbering63
|
||||
numbering/numbering87
|
||||
numbering/numbering92
|
||||
numbering/numbering93
|
||||
output/output13
|
||||
output/output15
|
||||
output/output17
|
||||
@ -243,13 +205,6 @@ sort/sort41
|
||||
string/string130
|
||||
string/string132
|
||||
string/string143
|
||||
string/string30
|
||||
string/string31
|
||||
string/string32
|
||||
string/string33
|
||||
string/string34
|
||||
string/string35
|
||||
string/string36
|
||||
variable/variable29
|
||||
variable/variable31
|
||||
variable/variable32
|
||||
|
||||
Loading…
Reference in New Issue
Block a user