XML technologies / XSLT / XSLT 3.0 / XSLT streaming / First steps with XSLT streaming / Burst mode
Streaming’s single pass by the XSLT processor leads to the streamability rules. Stylesheets often need a different structure, allowing only one downward expression. This is sometimes impossible or difficult, for example when reordering elements or accessing preceding or following information. (See Abel Braaksma, Lazy processing of XML in XSLT for big data. in XML London 2013 Conference Prodeedings, June 2013, XML London 2013, p. 136.)
Some restrictions can be bypassed with burst mode: store a file fragment in a variable and process its subtree in a separate non-streaming mode, restoring all ordinary capabilities. The file must split into independent pieces, usually easy for address lists, books or error logs.
Store the subtree using snapshot(.) and copy-of(.). Both save the current context and children. Function snapshot() also saves all ancestors and their attributes, preserving hierarchy access.
Input requirements:
The file must split into subtrees containing all information needed for processing.
Choose subtrees as large as necessary and as small as possible.
Each parsed fragment must fit in memory.
Caution: A parsed XML tree takes roughly 3–10 times the file’s memory. (See Michael Kay, Streaming in XSLT 2.1. in XML Prague 2010 Conference Prodeedings, February 2010, XML Prague 2010, p. 4.)
Burst mode processes files too large to load whole when independent fragments suffice. (See Abel Braaksma, Lazy processing of XML in XSLT for big data. in XML London 2013 Conference Prodeedings, June 2013, XML London 2013, p. 136.)
Processing time is linear in file size, so a fragment’s time estimates the whole file’s duration. Streaming makes this possible. (See Abel Braaksma, Lazy processing of XML in XSLT for big data. in XML London 2013 Conference Prodeedings, June 2013, XML London 2013, pp. 140-142.)
This example demonstrates burst mode.
Code example: Introductory burst-mode XSLT.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode streamable="yes" on-no-match="shallow-copy"/>
<xsl:mode streamable="no" on-no-match="shallow-copy" name="no-Streaming"/>
<xsl:template match="Name">
<xsl:variable name="copy" select="copy-of(.)"/>
<xsl:apply-templates select="$copy" mode="no-Streaming"/>
</xsl:template>
<xsl:template match="Name" mode="no-Streaming">
<xsl:copy>
<xsl:apply-templates select="Surname"/>
<xsl:apply-templates select="Firstname"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>A simple example reordering Firstname and Surname.
Line 2: Streaming mode for processing the file as a stream.
Line 3: Non-streaming mode for subtree processing.
Line 5: Element Name is stored in variable copy.
Line 6: The saved context is processed in mode no-Streaming.
Line 8: The template in mode no-Streaming matches element Name.
Line 10: The order of Firstname and Surname is swapped. All XSLT capabilities are available; treating the subtree as its own document removes streaming restrictions.
Burst-mode exercises
Address list (Exercise 4)
Play listing (Exercise 5)