Posts

Showing posts with the label merging

Fun(ctional programming) with fold-left and transform

Over on StackOverflow someone asked how to apply an XSLT 1.0 stylesheet for merging two XML documents to all XML files in a directory . The approach there in the question as well as the answer is to use a shell script to run the stylesheet with Saxon on two files, then merge the result with a third file and so on. However, given that since XPath 3.1 we have the uri-collection function  to process a sequence of files and that XPath 3.1 even offers a transform function to perform a transformation directly in XSLT/XPath I thought it should also be possible to solve the problem completely in XSLT 3.0. Additionally the algorithm to process a sequence of input files, applying the merge transformation repeatedly to each file, accumulating the result, looked like an opportunity to use the fold-left function supported also in XSLT 3.0 since being included in the XPath 3 functions and operators specification. The original XSLT 1.0 stylesheet takes a primary input document and a with ...

Using xsl:evaluate and the path() function to merge data from two documents with the same structure

On Stackoverflow there was a question to merge two documents with the same XML structure by copying text data from elements in a secondary input document to empty elements in the main input. I think this can be nicely done by using the path() function on empty elements together with the xsl:evaluate instruction (that is new in XSLT 3.0) to simply select and copy the text data from the secondary input document <xsl:template match="*[not(has-children())]"> <xsl:copy> <div class="merged"> <xsl:evaluate context-item="$doc2" xpath="path() || '/text()'"></xsl:evaluate> </div> </xsl:copy> </xsl:template> Full gist is The posted stylesheet works fine with Saxon 9.7 and later editions supporting xsl:evaluate (PE and EE) and with current versions of Altova XMLSpy/Raptor.