/// Top-level declaration helpers for ``XSLTParser``, kept in a separate file so /// the parser enum stays within the type-body and file length budgets. extension PureXML.XSLT.XSLTParser { /// Records an `xsl:namespace-alias`: the namespace bound to `stylesheet-prefix` /// is rewritten to the one bound to `result-prefix` (with that prefix) on output. static func addNamespaceAlias(_ child: XSLTTree, into parts: inout Parts) { let stylePrefix = XSLTNode.attribute(child, "stylesheet-prefix") ?? "#default" let resultPrefix = XSLTNode.attribute(child, "result-prefix") ?? "#default" let key = resolvePrefix(stylePrefix, at: child) ?? "" parts.namespaceAliases[key] = PureXML.XSLT.NamespaceAlias( uri: resolvePrefix(resultPrefix, at: child), prefix: resultPrefix == "#default" ? nil : resultPrefix, ) } /// Resolves a namespace prefix (or `#default`) to its URI from the `xmlns` /// declarations in scope at `node`, walking up to the stylesheet element. static func resolvePrefix(_ prefix: String, at node: XSLTTree) -> String? { var current: XSLTTree? = node while let element = current { for attribute in element.attributes { let isDefault = prefix == "#default" && attribute.name.prefix == nil && attribute.name.localName == "xmlns" let isPrefixed = attribute.name.prefix == "xmlns" && attribute.name.localName == prefix if isDefault || isPrefixed { return attribute.value } } current = element.parent } return nil } /// A `mode` attribute as expanded name `{uri}local` (bare local when /// unprefixed): a mode is compared by expanded name (XSLT 1.0 5.7), so two /// prefixes bound to one namespace are the same mode. nil when absent. static func expandedMode(_ node: XSLTTree) -> String? { guard let mode = XSLTNode.attribute(node, "mode") else { return nil } guard let colon = mode.firstIndex(of: ":") else { return mode } let prefix = String(mode[.. String { let raw = XSLTNode.attribute(node, "name") ?? "" guard let colon = raw.firstIndex(of: ":") else { return raw } let prefix = String(raw[.. PureXML.XSLT.DecimalFormat { var format = PureXML.XSLT.DecimalFormat() func char(_ name: String, _ keyPath: WritableKeyPath) { if let value = XSLTNode.attribute(node, name)?.first { format[keyPath: keyPath] = value } } char("decimal-separator", \.decimalSeparator) char("grouping-separator", \.groupingSeparator) char("percent", \.percent) char("per-mille", \.perMille) char("zero-digit", \.zeroDigit) char("digit", \.digit) char("pattern-separator", \.patternSeparator) char("minus-sign", \.minusSign) if let infinity = XSLTNode.attribute(node, "infinity") { format.infinity = infinity } if let notANumber = XSLTNode.attribute(node, "NaN") { format.notANumber = notANumber } return format } }