Reach full XSLT 1.0 key, numbering, and document parity: - xsl:key declarations are compiled into an index over the source tree; the key() function looks nodes up by key name and value. - format-number() renders a number against a picture string (digit places, decimal point, grouping, percent), Foundation-free. - xsl:number generates the context node's position rendered as arabic, alphabetic, or roman numerals. - document() resolves an external source through an injected loader so the transform never reaches the filesystem or network by default. These hang off an XPath function-table injection mechanism: the evaluator merges an engine-supplied FunctionTable, letting XSLT register current(), key(), format-number(), and document() per evaluation. Closes #63
99 lines
4.0 KiB
Swift
99 lines
4.0 KiB
Swift
public extension PureXML.XSLT {
|
|
/// One part of an attribute value template: literal text, or an embedded XPath
|
|
/// expression written `{expr}`.
|
|
enum ValuePart: Sendable {
|
|
case literal(String)
|
|
case expression(String)
|
|
}
|
|
|
|
/// An attribute value template (a string with `{expr}` substitutions).
|
|
typealias ValueTemplate = [ValuePart]
|
|
|
|
/// A literal-result-element attribute, whose value is an attribute value
|
|
/// template.
|
|
struct LiteralAttribute: Sendable {
|
|
public var name: PureXML.Model.QualifiedName
|
|
public var value: ValueTemplate
|
|
}
|
|
|
|
/// A sort specification for `apply-templates`/`for-each`.
|
|
struct Sort: Sendable {
|
|
public var select: String
|
|
public var descending: Bool
|
|
public var numeric: Bool
|
|
}
|
|
|
|
/// One instruction of a template body (a sequence constructor item).
|
|
indirect enum Instruction: Sendable {
|
|
/// Literal character data copied to the result.
|
|
case literalText(String)
|
|
/// `xsl:value-of`: the string value of an XPath expression.
|
|
case valueOf(select: String)
|
|
/// `xsl:apply-templates` with an optional node-set selection, mode, sorts,
|
|
/// and passed parameters.
|
|
case applyTemplates(select: String?, mode: String?, sorts: [Sort], parameters: [Binding])
|
|
/// `xsl:for-each` over a node-set.
|
|
case forEach(select: String, sorts: [Sort], body: [Instruction])
|
|
/// `xsl:if`.
|
|
case ifInstruction(test: String, body: [Instruction])
|
|
/// `xsl:choose` with its `when` branches and optional `otherwise`.
|
|
case choose(whens: [Branch], otherwise: [Instruction])
|
|
/// A literal result element, copied to the output with its attributes.
|
|
case literalElement(name: PureXML.Model.QualifiedName, attributes: [LiteralAttribute], body: [Instruction])
|
|
/// `xsl:element` whose name is an attribute value template.
|
|
case element(name: ValueTemplate, body: [Instruction])
|
|
/// `xsl:attribute` whose name is an attribute value template.
|
|
case attribute(name: ValueTemplate, body: [Instruction])
|
|
/// `xsl:copy`: a shallow copy of the context node.
|
|
case copy(body: [Instruction])
|
|
/// `xsl:copy-of`: a deep copy of a selected node-set.
|
|
case copyOf(select: String)
|
|
/// `xsl:call-template` by name with passed parameters.
|
|
case callTemplate(name: String, parameters: [Binding])
|
|
/// `xsl:variable` bound for the rest of the sequence constructor.
|
|
case variable(name: String, select: String?, body: [Instruction])
|
|
/// `xsl:number`: a generated sequence number for the context node.
|
|
case number(count: String?, from: String?, format: String)
|
|
}
|
|
|
|
/// An `xsl:key` declaration: a name, the nodes it indexes (a match pattern),
|
|
/// and the key value of each (a `use` expression).
|
|
struct Key: Sendable {
|
|
public var name: String
|
|
public var match: String
|
|
public var use: String
|
|
}
|
|
|
|
/// One `xsl:when` branch of a choose.
|
|
struct Branch: Sendable {
|
|
public var test: String
|
|
public var body: [Instruction]
|
|
}
|
|
|
|
/// A name binding: an `xsl:param` declaration (with a default) or an
|
|
/// `xsl:with-param` value, supplied either by `select` or by a body.
|
|
struct Binding: Sendable {
|
|
public var name: String
|
|
public var select: String?
|
|
public var body: [Instruction]
|
|
}
|
|
|
|
/// A template rule: a match pattern and/or a name, a mode, a priority, its
|
|
/// declared parameters, and a body.
|
|
struct Template: Sendable {
|
|
public var match: String?
|
|
public var name: String?
|
|
public var mode: String?
|
|
public var priority: Double
|
|
public var parameters: [Binding]
|
|
public var body: [Instruction]
|
|
}
|
|
|
|
/// A compiled stylesheet: its template rules, global variables, and keys.
|
|
struct Stylesheet: Sendable {
|
|
public var templates: [Template]
|
|
public var globals: [Instruction]
|
|
public var keys: [Key]
|
|
}
|
|
}
|