In Saxon 9.3 the xsl:result-document instruction is allowed inside an xsl:function, which should not happen.
FIX applied to the UserFunctionCall class in the method process method.
xsl:result-document should not be in an xs;:function according to the XSLT spec - (XSLT2.0 section 19.1 and XSLT 3.0 section 23.1):
Informally, the restriction is that the xsl:result-document instruction can only be used while writing a final result tree, not while writing to a temporary tree or a sequence. This restriction is defined formally as follows.
The instructions in the initial template are evaluated in final output state. An instruction is evaluated in the same output state as its calling instruction, except that xsl:variable, xsl:param, xsl:with-param, xsl:attribute, xsl:comment, xsl:processing-instruction, xsl:namespace, xsl:value-of, xsl:function, xsl:key, xsl:sort, and xsl:message always evaluate the instructions in their contained sequence constructor in temporary output state.