From e7a8f473ef3dac6d9ddd7263506b4003809038bb Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 24 Jun 2026 10:40:11 +0200 Subject: [PATCH] fix(xslt): evaluate the xsl:number format attribute as an AVT (#269) XSLT 1.0 7.7: the format attribute of xsl:number is an attribute value template. It was stored and used as a literal string, so format="{$f}" was never evaluated and a passed-in or computed format was ignored. Parse it as a ValueTemplate and evaluate it per instantiation. Closes Apache Xalan conformance case namedtemplate12. --- CHANGELOG.md | 2 ++ Sources/XSLT/XSLTElements.swift | 5 +++-- Sources/XSLT/XSLTModel.swift | 4 +++- Sources/XSLT/XSLTParserStructural.swift | 2 +- Tests/Fixtures/xalan-baseline.txt | 1 - Tests/XSLTNumberingTests.swift | 17 +++++++++++++++++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d3999..df2d74e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- The `format` attribute of `xsl:number` is now evaluated as an attribute value template (XSLT 1.0 7.7), so `format="{$f}"` uses the value of `$f` rather than the literal string. It was stored and used verbatim, so a computed or passed-in format was never applied. Closes Apache Xalan conformance case `namedtemplate12`. + - A large integer-valued number now converts to a string with no decimal point (XPath 1.0 4.2). The number-to-string formatter only treated a value below 1e15 as an integer, so an integer such as `1234567890123456` (still exact in a double, which represents integers exactly up to 2^53) printed as `1234567890123456.0`. The integer bound is now 2^63, the range in which the conversion is safe. Closes Apache Xalan conformance case `string132`. - A lone `/` (the root node) is now parsed correctly when it is followed by something other than the end of the expression, such as a `)`, `]`, `|`, or an operator. The XPath grammar makes the relative part after `/` optional, but the compiler only treated `/` as the root path at the very end of the expression, so `count(/)`, `generate-id(/)`, and `/ = .` tried to parse a step from the following token and yielded an empty node-set. The compiler now recognizes a `/` as the root whenever no location step follows it. Closes Apache Xalan conformance cases `position106` and `mdocs15`. diff --git a/Sources/XSLT/XSLTElements.swift b/Sources/XSLT/XSLTElements.swift index bd02a38..4e6ff64 100644 --- a/Sources/XSLT/XSLTElements.swift +++ b/Sources/XSLT/XSLTElements.swift @@ -225,6 +225,7 @@ extension PureXML.XSLT.Transformer { /// node, with patterns matched through the transform's match cache. func numberInstruction(_ instruction: PureXML.XSLT.Instruction, _ context: XSLTContext) -> String { guard case let .number(spec) = instruction else { return "" } + let format = avt(spec.format, context) let grouping = spec.groupingSeparator.flatMap { separator in spec.groupingSize.map { (separator: separator, size: $0) } } @@ -237,12 +238,12 @@ extension PureXML.XSLT.Transformer { guard number.isFinite, number.rounded() >= 1, number.rounded() <= Double(Int.max) else { return PureXML.XPath.Value.format(number) } - return XSLTNumbering.format([Int(number.rounded())], spec.format, grouping) + return XSLTNumbering.format([Int(number.rounded())], format, grouping) } let numbers = XSLTNumbering.numbers(of: context.node, level: spec.level, count: spec.count, from: spec.from) { node, pattern in matches(node, pattern) } - return XSLTNumbering.format(numbers, spec.format, grouping) + return XSLTNumbering.format(numbers, format, grouping) } } diff --git a/Sources/XSLT/XSLTModel.swift b/Sources/XSLT/XSLTModel.swift index dfd5250..a9ae9db 100644 --- a/Sources/XSLT/XSLTModel.swift +++ b/Sources/XSLT/XSLTModel.swift @@ -167,7 +167,9 @@ public extension PureXML.XSLT { var count: String? var from: String? var value: String? - var format: String + /// The `format` attribute is an attribute value template (XSLT 1.0 7.7), + /// evaluated per instantiation. + var format: ValueTemplate var groupingSeparator: String? var groupingSize: Int? } diff --git a/Sources/XSLT/XSLTParserStructural.swift b/Sources/XSLT/XSLTParserStructural.swift index ab17aea..23b4511 100644 --- a/Sources/XSLT/XSLTParserStructural.swift +++ b/Sources/XSLT/XSLTParserStructural.swift @@ -28,7 +28,7 @@ extension PureXML.XSLT.XSLTParser { count: XSLTNode.attribute(node, "count"), from: XSLTNode.attribute(node, "from"), value: XSLTNode.attribute(node, "value"), - format: XSLTNode.attribute(node, "format") ?? "1", + format: valueTemplate(XSLTNode.attribute(node, "format") ?? "1"), groupingSeparator: XSLTNode.attribute(node, "grouping-separator"), groupingSize: XSLTNode.attribute(node, "grouping-size").flatMap { Int($0) }, )) diff --git a/Tests/Fixtures/xalan-baseline.txt b/Tests/Fixtures/xalan-baseline.txt index 413e1ad..713f1e9 100644 --- a/Tests/Fixtures/xalan-baseline.txt +++ b/Tests/Fixtures/xalan-baseline.txt @@ -15,7 +15,6 @@ impincl/impincl08 impincl/impincl27 mdocs/mdocs07 mdocs/mdocs19 -namedtemplate/namedtemplate12 namespace/namespace113 namespace/namespace19 namespace/namespace23 diff --git a/Tests/XSLTNumberingTests.swift b/Tests/XSLTNumberingTests.swift index eaaa77c..bbe06d2 100644 --- a/Tests/XSLTNumberingTests.swift +++ b/Tests/XSLTNumberingTests.swift @@ -19,6 +19,23 @@ struct XSLTNumberingTests { return try PureXML.XSLT.transform(stylesheet: style, source: source) } + @Test("the format attribute is an attribute value template (7.7)") + func test_formatIsAVT() throws { + // format="{$f}" evaluates the AVT, so a passed-in format string drives the + // sequence; f="A. " numbers the items A. B. (Apache Xalan namedtemplate12). + let emit = "" + + "A. " + let style = """ + + + + \(emit) + 1. + + """ + #expect(try PureXML.XSLT.transform(stylesheet: style, source: "") == "A. B. ") + } + @Test("level=multiple numbers every matching ancestor-or-self, outermost first") func test_levelMultiple() throws { let source = """