Saxonica.com

Processing the nodes returned by saxon:stream()

The nodes selected by the streamed expression may be further processed. For example:

XSLT example



<xsl:template name="main">
  <xsl:apply-templates select="saxon:stream(doc('customers.xml')/*/customer)"
                       xmlns:saxon="http://saxon.sf.net/"/>
</xsl:template>

<xsl:template match="customer">
  <xsl:value-of select="code, name, location" separator="|"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

XQuery example


declare function f:customers() {
   saxon:stream(doc('customers.xml')/*/customer)
};

for $c in f:customers() 
return concat(string-join(($c/code, $c/name, $c/location), '|'), '&#xa;')

Conceptually, saxon:stream() evaluates the sequence supplied in its first argument as a sequence of nodes, and then makes copies of these nodes as described in the rules of the xsl:copy-of instruction. The significance of the (notional) copy operation is that the returned nodes have no ancestors or siblings; each is the root of its own tree.

The document that is processed in streaming mode must be read using the doc() function (or in XSLT, the document() function). The query or stylesheet may also process other documents (for example a document named on the command line) but this is not necessary. In XSLT it is often useful to activate the stylesheet at a named template using the -it option on the command line, which allows activation without a primary input document.

When streaming copy is used, the relevant calls on the doc() or document() functions are not stable: that is, there is no guarantee that if the same document is read more than once, its contents will be unchanged. This is because the whole point of the facility is to ensure that Saxon does not need to keep the content of the document in memory. This limitation explains the choice of the keyword read-once: the facility should not be used to process a document if it needs to be read more than once during the query or transformation.

Next