PureXML/Tests/XSLTDocumentBaseURITests.swift
Mihaela Mihaljevic b6af499558
fix(xslt): resolve document() references against the correct base URI (closes 3 Xalan reluri cases) (#256)
* fix(xslt): resolve document() references against the correct base URI

The document() function resolved every relative URI reference against the
single global base, so a reference relative to a document that document()
itself had loaded from a subdirectory could not be found. XSLT 1.0 12.1
defines the base per reference: the two-argument form against the second
argument's first node, the node-set form against each reference node's own
document.

Each loaded document now records the URI it came from as its base, and a
relative reference resolves against that base before the loader applies its
own source-document base. The common one-argument string form is unchanged
(its base is empty, deferring to the loader as before).

Closes Apache Xalan conformance cases reluri09, reluri10, reluri11.

* fix(xslt): record base URI for document() fragment selections

A fragment selection (document('x#frag')) is returned as a detached subtree
whose root identity differs from the cached whole-document tree, so its base
URI was not recorded. Using such a node as the base for a relative document()
reference then resolved against the empty base instead of the fragment's
document. Record the base on each fragment subtree's own root through a shared
helper, so baseURI(of:) finds it whichever node the reference is taken from.
2026-06-24 01:30:54 +02:00

56 lines
2.4 KiB
Swift

import Testing
@testable import PureXML
/// `document()` resolves a relative URI reference against a base URI (XSLT 1.0
/// 12.1): the second argument's first node supplies the base for the two-argument
/// form, and a node's own document supplies it for the node-set form. A document
/// loaded from `dir/sub/inner.xml` lends its `dir/sub/` directory to references
/// taken relative to it.
@Suite("XSLT document() base URI")
struct XSLTDocumentBaseURITests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
/// A loader over a fixed in-memory directory tree, exact path to content.
private static let files = [
"dir/sub/inner.xml": "<inner><ref>sibling.xml</ref></inner>",
"dir/sub/target.xml": "<hit>TWO_ARG</hit>",
"dir/sub/sibling.xml": "<sib>NODE_SET</sib>",
]
private func run(_ select: String) throws -> String {
try PureXML.XSLT.transform(
stylesheet: """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:value-of select="\(select)"/></out></xsl:template>
</xsl:stylesheet>
""",
source: "<doc/>",
documentLoader: { Self.files[$0] },
)
}
@Test("two-argument form resolves against the second argument's base URI")
func test_twoArgument() throws {
// `target.xml` resolves against `dir/sub/inner.xml`, reaching dir/sub/target.xml.
let out = try run("document('target.xml', document('dir/sub/inner.xml'))/hit")
#expect(out == "<out>TWO_ARG</out>")
}
@Test("node-set form resolves against the referencing node's own base URI")
func test_nodeSet() throws {
// The <ref> node lives in dir/sub/inner.xml, so its `sibling.xml` value
// resolves to dir/sub/sibling.xml.
let out = try run("document(document('dir/sub/inner.xml')/inner/ref)/sib")
#expect(out == "<out>NODE_SET</out>")
}
@Test("a fragment-selected node still carries its document's base URI")
func test_fragmentBase() throws {
// The fragment selection is a detached subtree, yet it must still resolve
// `target.xml` against dir/sub/inner.xml's directory.
let out = try run("document('target.xml', document('dir/sub/inner.xml#xpointer(/inner)'))/hit")
#expect(out == "<out>TWO_ARG</out>")
}
}