PureXML/Tests/XSLTNamespaceAliasTests.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

53 lines
2.2 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, and the serializer declares the result namespace.
#expect(try transform(style, "<x/>") == "<b:thing xmlns:b=\"urn:to\"/>")
}
@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"))
}
}