PureXML/Sources/XSLT/XSLTParserStructural.swift
Mihaela Mihaljevic e5402f8bf5 feat(xslt): namespaced name creation, copy attribute sets, result fixup
xsl:attribute/xsl:element honor the namespace attribute and resolve
prefixed names against the instruction's in-scope stylesheet
declarations (captured at parse); xsl:copy applies use-attribute-sets;
and a namespace-fixup pass before serialization declares every
namespace a created name carries, reusing same-URI bindings or
generating ns0.. prefixes, and undeclares an inherited default that
would capture an unqualified name. Xalan baseline 604 to 557.
2026-06-10 12:10:48 +02:00

57 lines
2.3 KiB
Swift

extension PureXML.XSLT.XSLTParser {
static func structuralInstruction(_ node: XSLTTree) -> PureXML.XSLT.Instruction? {
switch XSLTNode.localName(node) {
case "for-each":
.forEach(select: XSLTNode.attribute(node, "select") ?? "", sorts: sorts(node), body: body(node))
case "if":
.ifInstruction(test: XSLTNode.attribute(node, "test") ?? "", body: body(node))
case "choose":
choose(node)
case "element":
.element(
name: valueTemplate(XSLTNode.attribute(node, "name") ?? ""),
namespace: XSLTNode.attribute(node, "namespace").map(valueTemplate),
namespaces: inScopeNamespaces(node),
useAttributeSets: useAttributeSets(node),
body: body(node),
)
case "attribute":
.attribute(
name: valueTemplate(XSLTNode.attribute(node, "name") ?? ""),
namespace: XSLTNode.attribute(node, "namespace").map(valueTemplate),
namespaces: inScopeNamespaces(node),
body: body(node),
)
case "number":
.number(
count: XSLTNode.attribute(node, "count"),
from: XSLTNode.attribute(node, "from"),
format: XSLTNode.attribute(node, "format") ?? "1",
)
case "comment":
.comment(body: body(node))
case "processing-instruction":
.processingInstruction(name: valueTemplate(XSLTNode.attribute(node, "name") ?? ""), body: body(node))
default:
nil
}
}
/// The namespace bindings in scope at a stylesheet node, for resolving
/// the prefix of a created name (7.1.2/7.1.3: the QName in a name
/// attribute is expanded using the instruction's own declarations).
static func inScopeNamespaces(_ node: XSLTTree) -> [String: String] {
var bindings: [String: String] = [:]
var current: XSLTTree? = node
while let candidate = current {
for attribute in candidate.attributes where attribute.name.prefix == "xmlns" {
if bindings[attribute.name.localName] == nil {
bindings[attribute.name.localName] = attribute.value
}
}
current = candidate.parent
}
return bindings
}
}