XML technologies / XSLT / XSLT 3.0 / XSLT streaming / Elements / xsl:merge

With xsl:merge, multiple input files can be merged, for example error logs from different systems.

Individual input files, including several XML files with different structures, are specified with the xsl:merge-source element.

When merging the files, entries are sorted. The sort order is specified in <xsl:merge-key select="@timestamp">.

When using merge in streaming, all input files must already be sorted. They cannot be sorted before the merge. Attempting to merge unsorted files aborts the transformation.

Code example Introductory xsl:merge example (XSLT) shows how to merge the two differently structured log files from code examples Introductory xsl:merge example (input 1) and Introductory xsl:merge example (input 2).

 

Code example: introductory xsl:merge example (input 1).

<events>
  <event timestamp="2009-08-20T12:01:01Z">A1 Transaction T1234 started</event>
  <event timestamp="2009-08-20T12:01:08Z">A1 Transaction T1235 started</event>
  <event timestamp="2009-09-20T12:01:12Z">A1 Transaction T1235 ended</event>
  <event timestamp="2009-09-20T12:01:15Z">A1 Transaction T1234 ended</event>
</events>

Input file 1 for merging log entries.

 

Code example: introductory xsl:merge example (input 2).

<log>
  <day>
    <record time="2009-08-20T12:01:01Z">B1 Transaction T1234 started</record>
    <record time="2009-08-20T12:01:08Z">B1 Transaction T1235 started</record>
  </day>
  <day>
    <record time="2009-09-20T12:01:12Z">B1 Transaction T1235 ended</record>
    <record time="2009-09-20T12:01:15Z">B1 Transaction T1234 ended</record>
  </day>
</log>

Input file 2, with a different structure, for merging log entries.

 

Code example: introductory xsl:merge example (XSLT).

<xsl:template match="events">
  <Log>
    <xsl:merge> 
      <xsl:merge-source for-each-source="'../in/Log_A_1.xml', '../in/Log_A_2.xml'" select="/events/event"> 
        <xsl:merge-key select="@timestamp"/>
      </xsl:merge-source> 
      <xsl:merge-source for-each-source="'../in/Log_B_1.xml'" select="/log/day/record">  
        <xsl:merge-key select="@time"/> 
      </xsl:merge-source> 
      <xsl:merge-action> 
        <xsl:apply-templates select="current-merge-group()"/> 
      </xsl:merge-action> 
    </xsl:merge> 
  </Log> 
</xsl:template>

Example merging log files Log_A_1.xml, Log_A_2.xml and Log_B_1.xml. The two “A” files have a different structure from the “B” file.

Line 3: start of the xsl:merge instruction.

Line 4: specifies the “A” input files for the merge.

Line 5: specifies the merge key by which the “A” input files are already sorted.

Line 7: specifies the “B” input file. This is necessary because its structure differs from the “A” files.

Line 8: specifies the merge key for the “B” input file.

Line 10: specifies the action to perform on the merged elements.

Line 11: with current-merge-group(), the merged element can be processed further.

Exercises on xsl:merge

   

back to top