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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-24 10:40:11 +02:00 committed by GitHub
parent 95fde5b177
commit e7a8f473ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 5 deletions

View File

@ -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`.

View File

@ -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)
}
}

View File

@ -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?
}

View File

@ -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) },
))

View File

@ -15,7 +15,6 @@ impincl/impincl08
impincl/impincl27
mdocs/mdocs07
mdocs/mdocs19
namedtemplate/namedtemplate12
namespace/namespace113
namespace/namespace19
namespace/namespace23

View File

@ -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 = "<xsl:call-template name=\"n\">"
+ "<xsl:with-param name=\"f\">A. </xsl:with-param></xsl:call-template>"
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:apply-templates select="doc/i"/></out></xsl:template>
<xsl:template match="i">\(emit)</xsl:template>
<xsl:template name="n"><xsl:param name="f">1. </xsl:param><xsl:number format="{$f}"/></xsl:template>
</xsl:stylesheet>
"""
#expect(try PureXML.XSLT.transform(stylesheet: style, source: "<doc><i/><i/></doc>") == "<out>A. B. </out>")
}
@Test("level=multiple numbers every matching ancestor-or-self, outermost first")
func test_levelMultiple() throws {
let source = """