Search code examples
xmlxsltmsxml

Concatenating xml files


I have several xml files, the names of which are stored in another xml file.

I want to use xsl to produce a summary of the combination of the xml files. I remember there was a way to do this with the msxml extensions (I'm using msxml).

I know I can get the content of each file using select="document(filename)" but I'm not sure how to combine all these documents into one.

21-Oct-08 I should have mentioned that I want to do further processing on the combined xml, so it is not sufficient to just output it from the transform, I need to store it as a node set in a variable.


Solution

  • Here is just a small example of what you could do:

    file1.xml:

    <foo>
    <bar>Text from file1</bar>
    </foo>
    

    file2.xml:

    <foo>
    <bar>Text from file2</bar>
    </foo>
    

    index.xml:

    <index>
    <filename>file1.xml</filename>
    <filename>file2.xml</filename>
    

    summarize.xsl:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:exsl="http://exslt.org/common"
        extension-element-prefixes="exsl">
    
      <xsl:variable name="big-doc-rtf">
          <xsl:for-each select="/index/filename">
            <xsl:copy-of select="document(.)"/>
          </xsl:for-each>
      </xsl:variable>
    
      <xsl:variable name="big-doc" select="exsl:node-set($big-doc-rtf)"/>
    
      <xsl:template match="/">
        <xsl:element name="summary">
          <xsl:apply-templates select="$big-doc/foo"/>
        </xsl:element>  
      </xsl:template>
    
      <xsl:template match="foo">
        <xsl:element name="text">
          <xsl:value-of select="bar"/>
        </xsl:element>  
      </xsl:template>
    
    </xsl:stylesheet>
    

    Applying the stylesheet to index.xml gives you:

    <?xml version="1.0" encoding="UTF-8"?><summary><text>Text from file1</text><text>Text from file2</text></summary>
    

    The trick is to load the different documents with the document function (extension function supported by almost all XSLT 1.0 processors), to output the contents as part of a variable body and then to convert the variable to a node-set for further processing.