XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:fork
The element xsl:fork allows processing to be split across multiple processes. Each construct in the xsl:fork instruction runs in a separate process.
A xsl:fork can contain either one xsl:for-each-group and an optional xsl:fallback, or zero or more xsl:sequence constructs, each with an optional xsl:fallback.
The result of a xsl:fork instruction is the concatenation of all contained instructions in the order they occur.
With xsl:fork it is therefore possible to stream constructs that would otherwise not be streamable, because each construct is handled in its own process during the pass.
How xsl:fork works is shown in the following code example.
Code example: Introductory example of xsl:fork.
<xsl:template match="Company">
<xsl:fork>
<xsl:sequence> </xsl:sequence>
<xsl:sequence> </xsl:sequence>
<xsl:fork>
</xsl:template>Two xsl:sequence instructions, each running in its own process.
Line 3: Instruction in its own process.
Line 4: Instruction in another separate process.
Streaming rules
None of the branches above can return a streamed node, since they may not be processed in the correct order and reordering is impossible during streaming. You should therefore always use grounded nodes.
Tip
As a rule of thumb, always use select:"copy-of(x)" instead of select="x" in a fork. See the following two code examples:
Code example: xsl:fork - permitted.
<xsl:fork>
<xsl:sequence select="copy-of(author)"/>
<xsl:sequence select="copy-of(editor)"/>
</xsl:fork>The function copy-of() allows parallel access to elements within streams.
Code example: xsl:fork - not permitted.
<xsl:fork>
<xsl:sequence select="author"/>
<xsl:sequence select="editor"/>
</xsl:fork>This example is not permitted because streamed nodes may be returned in an order that differs from document order.
Exercise on xsl:fork
Splitting an employee list (Exercise 11)