PureXML/Tests/XSLTResultFragmentTests.swift
Mihaela Mihaljevic 4ea84b3d58 feat(xslt): make result-tree fragments usable as node-sets
An xsl:variable with a body now binds a queryable document fragment (its
instantiated result converted to a TreeNode document) instead of a plain
string, so count($rtf/*), $rtf/child, and for-each over $rtf/... work.
It still has its concatenated text as a string value, so existing string
uses are unchanged. Part of #82.
2026-06-09 03:04:56 +02:00

58 lines
1.9 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSLT result-tree fragment as node-set")
struct XSLTResultFragmentTests {
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)
}
private func body(_ select: String) -> String {
"""
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="rtf"><a>1</a><b>2</b><a>3</a></xsl:variable>
<xsl:value-of select="\(select)"/>
</xsl:template>
</xsl:stylesheet>
"""
}
@Test("count($rtf/*) counts the fragment's top-level elements")
func test_countAll() throws {
#expect(try transform(body("count($rtf/*)"), "<x/>") == "3")
}
@Test("A name test selects matching fragment children")
func test_nameTest() throws {
#expect(try transform(body("count($rtf/a)"), "<x/>") == "2")
}
@Test("A path into the fragment reads a child's value")
func test_childValue() throws {
#expect(try transform(body("$rtf/b"), "<x/>") == "2")
}
@Test("The fragment still has a string value (its concatenated text)")
func test_stringValue() throws {
#expect(try transform(body("string($rtf)"), "<x/>") == "123")
}
@Test("for-each iterates the fragment's nodes")
func test_forEach() throws {
let style = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="rtf"><a>1</a><b>2</b><a>3</a></xsl:variable>
<xsl:for-each select="$rtf/a"><xsl:value-of select="."/><xsl:text>,</xsl:text></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
"""
#expect(try transform(style, "<x/>") == "1,3,")
}
}