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...