PureXML/Tests/XSLTNamespaceAliasTests.swift
Mihaela Mihaljevic 610b7d0ca0 feat(xslt): implement xsl:namespace-alias, completing #81
A literal result element and its attributes in a stylesheet namespace
bound by stylesheet-prefix are rewritten on output to the namespace and
prefix bound by result-prefix (#default supported), so a stylesheet can
emit literal xsl: elements. Prefixes resolve through the in-scope xmlns
declarations; aliases fold across include/import. Splits AVT parsing into
XSLTAVT.swift and trims the transformer to stay within length budgets.
2026-06-09 02:10:16 +02:00

52 lines
2.1 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSLT xsl:namespace-alias")
struct XSLTNamespaceAliasTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ stylesheet: String, _ source: String) throws -> String {
try PureXML.XSLT.transform(stylesheet: stylesheet, source: source)
}
@Test("A literal element in the aliased namespace is rewritten to the result namespace")
func test_rewrite() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl) xmlns:a="urn:from" xmlns:b="urn:to">
<xsl:namespace-alias stylesheet-prefix="a" result-prefix="b"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><a:thing/></xsl:template>
</xsl:stylesheet>
"""
// The element's prefix is rewritten from the stylesheet alias to the result.
#expect(try transform(style, "<x/>") == "<b:thing/>")
}
@Test("A non-aliased literal element is untouched")
func test_notAliased() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl) xmlns:a="urn:from" xmlns:b="urn:to">
<xsl:namespace-alias stylesheet-prefix="a" result-prefix="b"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><plain/></xsl:template>
</xsl:stylesheet>
"""
#expect(try transform(style, "<x/>") == "<plain/>")
}
@Test("namespace-alias can produce literal xsl: elements (stylesheet generation)")
func test_generateXSL() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl) xmlns:axsl="urn:alias">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><axsl:value-of select="."/></xsl:template>
</xsl:stylesheet>
"""
let out = try transform(style, "<x/>")
// The aliased axsl: element is emitted as a literal xsl: element, not run.
#expect(out.contains("xsl:value-of"))
#expect(!out.contains("axsl"))
}
}