It seems with XSLT 3.0 you can catch the error to write to a output URI twice using xsl:try/xsl:catch
, given the stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:mode streamable="yes"/>
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="copy-of(root/record)" mode="result"/>
</xsl:template>
<xsl:template match="record" mode="result">
<xsl:try>
<xsl:result-document href="{fname}.txt" method="text">
<xsl:value-of select="* except fname" separator=","/>
</xsl:result-document>
<xsl:catch errors="err:XTDE1490">
<xsl:message select="'Attempt to write more than once to ', fname"/>
</xsl:catch>
</xsl:try>
</xsl:template>
</xsl:stylesheet>
and an input like
<?xml version="1.0" encoding="UTF-8"?>
<root>
<record>
<fname>result1</fname>
<foo>1</foo>
<bar>a</bar>
</record>
<record>
<fname>result2</fname>
<foo>2</foo>
<bar>b</bar>
</record>
<record>
<fname>result1</fname>
<foo>1</foo>
<bar>a</bar>
</record>
</root>
Saxon 9.8 EE processes the input with streaming and writes two result files while catching the error when trying to write a second time to result1.txt
when processing the third record.
As for @MichaelKay's comment about the implementation dependency as to which duplicates will be caught, I agree with that, but if it matters to avoid that then we can simply replace
<xsl:template match="/">
<xsl:apply-templates select="copy-of(root/record)" mode="result"/>
</xsl:template>
with a use of xsl:iterate
<xsl:template match="/">
<xsl:iterate select="root/record">
<xsl:apply-templates select="copy-of()" mode="result"/>
</xsl:iterate>
</xsl:template>
that way I think sequential processing is done.