Extracting sub trees of a document using snapshot()

In this post I will explore how the new snapshot function in XSLT 3.0 makes life easier with tasks like extracting sub trees from a document, for instance to split up a file into several, each containing a sub tree from the original file.
So let's assume we have some input data as follows:

and we want to split it up into several documents, each containing a page element in its sub tree, for instance:


With XSLT 2.0 we would have to reconstruct the sub tree a page element is contained in, now with XSLT 3.0's snapshot function it is as easy as:


So we simply process each page element and in the matching template we create a result document containing the sub tree of the page by using

        <xsl:result-document href="pages/page{position()}.xml">
            <xsl:copy-of select="root(snapshot())"/>
        </xsl:result-document>

It is also easy to adapt the stylesheet to work with streaming, to make our apply-templates streamable we simply need to change the selection to use the outermost function and to process the input with streaming we set up the mode as streamable:


Now an XSLT 3.0 processor supporting streaming, like Saxon 9.8 EE does, will process the input document with streaming and process the input document and split it up into sub trees, without building the complete input tree, as an XSLT 2.0 processor or an XSLT 3.0 processor not supporting streaming would do.



Comments

Popular posts from this blog

Using accumulators to number items in a streamable way

Fun(ctional programming) with fold-left and transform