diff --git a/CHANGELOG.md b/CHANGELOG.md index 104d943..bf8e273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Globals, prefix wildcards, fifteenth xalan burn-down (#130): a top-level variable now evaluates with every previously evaluated global in scope, so a variable whose body references an earlier top-level param (directly or via call-template) resolves; same-name globals resolve by import precedence (folds happen in precedence order and the last same-name entry wins, so a later sibling import beats an earlier one and the importing sheet beats both); the stylesheet fold uniformly favors later contributions for attribute-set definitions, decimal formats, namespace aliases, and output settings, matching the post-order precedence model; and the XPath compiler accepts the `NCName:*` name-test form, matching any name in the prefix's namespace (bound by URI, falling back to the in-document prefix string) at its -0.25 default priority, so `@ped:*` outranks `@*`. Three unit tests pin the behaviors. Baseline 144 to 134 (variable 10 to 5, conflictres 8 to 5). - Import precedence and relative composition, fourteenth xalan burn-down (#130): import precedences are assigned in post-order over the import tree, so sibling imports are distinct (later higher) and every unit sits above its own imports; apply-imports searches only the current template's stylesheet's own import subtree per 5.6 (templates carry their unit's precedence range), falling back 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 own URI, so nested relative chains load correctly. Also fixed along the way: an included document's tree was only weakly referenced once flattened, so its ancestor chain (xmlns declarations, exclusions in scope) could disappear before its templates were parsed. Two unit tests pin apply-imports scoping and relative chains. Baseline 159 to 144 (reluri 11 to 3, impincl 9 to 2). - Attribute sets, copied namespaces, html booleans, loader entities, thirteenth xalan burn-down (#130): same-name `xsl:attribute-set` declarations merge as ordered definitions per 7.1.4 (each expands its used sets before its own attributes, a later definition's attributes winning); `xsl:copy` of an element carries the source element's in-scope namespace nodes per 7.5 (the fixup pass drops result-redundant ones); the namespace fixup lets a carried attribute prefix shadow an inherited binding locally (matching the source document's shape) unless the element's own name relies on it; the html method minimizes boolean attributes whose value repeats their name (CHECKED="CHECKED" serializes as CHECKED); and external DTDs and entities referenced by the stylesheet or source now resolve through the transform's document loader, the same channel as document(). Four unit tests pin the new behaviors. Baseline 174 to 159 (attribset 13 to 4, copy 12 to 7). diff --git a/README.md b/README.md index defc187..bd7bff1 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ flowchart TB PureXML is a working, dependency-free XML library today: parse, emit, validate, query, and stream documents on macOS, Linux, Windows, and WASM. The test suite -currently runs **972 tests in 166 suites** (`swift test`). +currently runs **975 tests in 167 suites** (`swift test`). ### Shipped (libxml2-aligned surface) diff --git a/Sources/XPath/CompilerPaths.swift b/Sources/XPath/CompilerPaths.swift index b0010d9..c018548 100644 --- a/Sources/XPath/CompilerPaths.swift +++ b/Sources/XPath/CompilerPaths.swift @@ -202,8 +202,14 @@ extension PureXML.XPath.Compiler { advance() return .wildcard } - let name = parseName() + var name = parseName() guard !name.isEmpty else { throw QueryError.expectedNodeTest } + if name.hasSuffix(":"), peek() == "*" { + // The NCName:* form: any name in the prefix's namespace. + advance() + name += "*" + return .name(name) + } guard peek() == "(" else { return .name(name) } diff --git a/Sources/XPath/EvaluatorSteps.swift b/Sources/XPath/EvaluatorSteps.swift index b27aa04..21846e0 100644 --- a/Sources/XPath/EvaluatorSteps.swift +++ b/Sources/XPath/EvaluatorSteps.swift @@ -104,6 +104,8 @@ extension PureXML.XPath.Evaluator { let prefix = String(name[..() + let resolvedGlobals: [PureXML.XSLT.Instruction] = globals.reversed().filter { instruction in + guard let name = Self.globalName(instruction) else { return true } + return seen.insert(name).inserted + }.reversed() + return PureXML.XSLT.Stylesheet( templates: templates, - globals: globals, + globals: resolvedGlobals, keys: keys, output: output, stripSpace: stripSpace, @@ -24,19 +32,28 @@ struct Parts { ) } - /// Folds a sub-stylesheet in: an import has lower precedence, so its globals - /// come before and its output is overridden by this stylesheet's. - mutating func fold(_ sub: PureXML.XSLT.Stylesheet?, isImport: Bool) { + private static func globalName(_ instruction: PureXML.XSLT.Instruction) -> String? { + guard case let .variable(name, _, _) = instruction else { return nil } + return name + } + + /// Folds a sub-stylesheet in. Folds happen in import-precedence order + /// (earlier imports first, the unit's own declarations absorbed last), + /// so later contributions uniformly take precedence: globals append (the + /// stylesheet getter keeps the last same-name entry), attribute-set + /// definitions concatenate (later ones expand later and win), and the + /// later output settings layer on top. + mutating func fold(_ sub: PureXML.XSLT.Stylesheet?, isImport _: Bool) { guard let sub else { return } templates += sub.templates keys += sub.keys stripSpace.formUnion(sub.stripSpace) preserveSpace.formUnion(sub.preserveSpace) - attributeSets.merge(sub.attributeSets) { mine, _ in mine } - decimalFormats.merge(sub.decimalFormats) { mine, _ in mine } - namespaceAliases.merge(sub.namespaceAliases) { mine, _ in mine } - globals = isImport ? sub.globals + globals : globals + sub.globals - output = isImport ? sub.output.merged(with: output) : output.merged(with: sub.output) + attributeSets.merge(sub.attributeSets) { mine, theirs in mine + theirs } + decimalFormats.merge(sub.decimalFormats) { _, theirs in theirs } + namespaceAliases.merge(sub.namespaceAliases) { _, theirs in theirs } + globals += sub.globals + output = output.merged(with: sub.output) } } diff --git a/Sources/XSLT/XSLTTransformer.swift b/Sources/XSLT/XSLTTransformer.swift index 636b2c9..c32b45f 100644 --- a/Sources/XSLT/XSLTTransformer.swift +++ b/Sources/XSLT/XSLTTransformer.swift @@ -59,10 +59,13 @@ extension PureXML.XSLT { func run() -> PureXML.Model.Node { var variables: [String: PureXML.XPath.Value] = [:] - let baseContext = XSLTContext(node: root, position: 1, size: 1, variables: variables) for global in stylesheet.globals { if case let .variable(name, select, body) = global { - variables[name] = variableValue(select, body, baseContext) + // Each global evaluates with every previously evaluated + // global in scope (a top-level variable may reference an + // earlier top-level param, directly or via call-template). + let globalContext = XSLTContext(node: root, position: 1, size: 1, variables: variables) + variables[name] = variableValue(select, body, globalContext) } } let context = XSLTContext(node: root, position: 1, size: 1, variables: variables) diff --git a/Tests/Fixtures/xalan-baseline.txt b/Tests/Fixtures/xalan-baseline.txt index fb64673..bb31aec 100644 --- a/Tests/Fixtures/xalan-baseline.txt +++ b/Tests/Fixtures/xalan-baseline.txt @@ -4,8 +4,6 @@ attribset/attribset27 attribset/attribset29 attribset/attribset44 -attribset/attribset45 -attribset/attribset46 attribset/attribset48 attribvaltemplate/attribvaltemplate08 axes/axes104 @@ -18,14 +16,11 @@ axes/axes126 axes/axes128 boolean/boolean43 boolean/boolean87 -conflictres/conflictres19 -conflictres/conflictres20 conflictres/conflictres22 conflictres/conflictres23 conflictres/conflictres29 conflictres/conflictres30 conflictres/conflictres34 -conflictres/conflictres37 copy/copy09 copy/copy27 copy/copy48 @@ -132,16 +127,11 @@ sort/sort33 sort/sort34 string/string132 string/string143 -variable/variable29 -variable/variable31 -variable/variable32 variable/variable33 variable/variable34 variable/variable35 variable/variable41 variable/variable55 -variable/variable67 -variable/variable70 whitespace/whitespace06 whitespace/whitespace07 whitespace/whitespace13 diff --git a/Tests/XSLTCompositionTests.swift b/Tests/XSLTCompositionTests.swift new file mode 100644 index 0000000..8667b0a --- /dev/null +++ b/Tests/XSLTCompositionTests.swift @@ -0,0 +1,157 @@ +@testable import PureXML +import Testing + +/// Stylesheet composition behaviors of the xalan burn-down (#130): import +/// precedence and apply-imports scoping, relative include/import chains, +/// global variable scope and precedence, attribute-set definition merging, +/// xsl:copy namespace-node copying, and NCName:* name tests. +@Suite("XSLT composition and scoping") +struct XSLTCompositionTests { + private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"" + + @Test("apply-imports searches only the current stylesheet's own imports (5.6)") + func test_applyImportsScope() throws { + let leaf = """ + + + + """ + let sibling = """ + + + + """ + let style = """ + + + + + + + """ + let result = try PureXML.XSLT.transform( + stylesheet: style, + source: "x", + documentLoader: { ["leaf.xsl": leaf, "sibling.xsl": sibling][$0] }, + ) + // sibling.xsl wins (later import); its apply-imports has no imports of + // its own, so the built-in rule runs, not leaf.xsl's template. + #expect(result == "x") + } + + @Test("Include and import hrefs resolve against the including stylesheet's URI") + func test_relativeIncludeChain() throws { + let inner = """ + + + + """ + let middle = """ + + + + """ + let style = """ + + + + + + """ + let result = try PureXML.XSLT.transform( + stylesheet: style, + source: "", + documentLoader: { ["sub/middle.xsl": middle, "sub/inner.xsl": inner][$0] }, + ) + #expect(result == "") + } + + @Test("A global variable sees earlier globals, call-template included") + func test_globalSeesEarlierGlobals() throws { + let style = """ + + + + + + + + """ + #expect(try PureXML.XSLT.transform(stylesheet: style, source: "") == "titi") + } + + @Test("Same-name globals resolve by import precedence, later sibling import winning") + func test_globalImportPrecedence() throws { + let first = """ + + """ + let second = """ + + """ + let style = """ + + + + + + + """ + let result = try PureXML.XSLT.transform( + stylesheet: style, + source: "", + documentLoader: { ["first.xsl": first, "second.xsl": second][$0] }, + ) + #expect(result == "second") + } + + @Test("NCName:* name tests match by namespace and outrank the bare wildcard") + func test_prefixWildcard() throws { + let style = """ + + + + w, + q, + + """ + let source = "" + #expect(try PureXML.XSLT.transform(stylesheet: style, source: source) == "w,q,") + } + + @Test("Same-name attribute sets merge as ordered definitions (7.1.4)") + func test_attributeSetMerge() throws { + let style = """ + + + + yellowbrickroad + shallow + + + theleader + + deep + ondrugs + + + """ + // The later definition expands after the earlier one (its used set + // included), so its hole=deep and follow=theleader win. + #expect(try PureXML.XSLT.transform(stylesheet: style, source: "") + == "") + } + + @Test("xsl:copy carries the source element's in-scope namespace nodes (7.5)") + func test_copyNamespaceNodes() throws { + let style = """ + + + + + + """ + let source = "" + #expect(try PureXML.XSLT.transform(stylesheet: style, source: source) + == "") + } +} diff --git a/Tests/XSLTOutputMethodTests.swift b/Tests/XSLTOutputMethodTests.swift index b0ad5fc..aead8b0 100644 --- a/Tests/XSLTOutputMethodTests.swift +++ b/Tests/XSLTOutputMethodTests.swift @@ -123,43 +123,6 @@ struct XSLTOutputMethodTests { #expect(try PureXML.XSLT.transform(stylesheet: style, source: source) == "inner c

a

") } - @Test("Same-name attribute sets merge as ordered definitions (7.1.4)") - func test_attributeSetMerge() throws { - let style = """ - - - - yellowbrickroad - shallow - - - theleader - - deep - ondrugs - - - """ - // The later definition expands after the earlier one (its used set - // included), so its hole=deep and follow=theleader win. - #expect(try PureXML.XSLT.transform(stylesheet: style, source: "") - == "") - } - - @Test("xsl:copy carries the source element's in-scope namespace nodes (7.5)") - func test_copyNamespaceNodes() throws { - let style = """ - - - - - - """ - let source = "" - #expect(try PureXML.XSLT.transform(stylesheet: style, source: source) - == "") - } - @Test("html boolean attributes minimize when the value repeats the name") func test_htmlBooleanAttributes() throws { let style = """ @@ -172,63 +135,6 @@ struct XSLTOutputMethodTests { == "
") } - @Test("apply-imports searches only the current stylesheet's own imports (5.6)") - func test_applyImportsScope() throws { - let leaf = """ - - - - """ - let sibling = """ - - - - """ - let style = """ - - - - - - - """ - let result = try PureXML.XSLT.transform( - stylesheet: style, - source: "x", - documentLoader: { ["leaf.xsl": leaf, "sibling.xsl": sibling][$0] }, - ) - // sibling.xsl wins (later import); its apply-imports has no imports of - // its own, so the built-in rule runs, not leaf.xsl's template. - #expect(result == "x") - } - - @Test("Include and import hrefs resolve against the including stylesheet's URI") - func test_relativeIncludeChain() throws { - let inner = """ - - - - """ - let middle = """ - - - - """ - let style = """ - - - - - - """ - let result = try PureXML.XSLT.transform( - stylesheet: style, - source: "", - documentLoader: { ["sub/middle.xsl": middle, "sub/inner.xsl": inner][$0] }, - ) - #expect(result == "") - } - @Test("Stylesheet and source entities resolve through the document loader") func test_loaderEntityResolution() throws { let style = """