XSLT: Reading Multiple Files with the document() Function
This tip demonstrates how to include several different XML documents using XSLT. This is done using the document function of XSLT. In this example, we want to combine three files that contain a name an e-mail address into one output document. Here is an example of one of the input files.
Listing for: mail1.xml
1 <entry>
2 <name>John Doe</name>
3 <address>jdoe@example1.com</address>
4 </entry>
To include this file and the other two, we need to include references to the files in an XML file that summarizes all the files to be included. Our stylesheet will run against this file.
Listing for: maillist.xml
1 <mail_list>
2 <file name="mail1.xml" />
3 <file name="mail2.xml" />
4 <file name="mail3.xml" />
5 </mail_list>
Finally, we need to write an XSLT stylesheet which reads the maillist.xml file and incorporates mail1.xml, mail2.xml, and mail3.xml.
Listing for: mergedocs.xsl
1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
2 <xsl:output method="text"/>
3 <xsl:template match="/">
4 <xsl:for-each select="/mail_list/file">
5 <xsl:apply-templates select="document(@name)/entry" />
6 </xsl:for-each>
7 </xsl:template>
8
9 <xsl:template match="entry">
10 <xsl:apply-templates />
11 </xsl:template>
12
13 <xsl:template match="name">Name: <xsl:value-of select="." />
14 </xsl:template>
15
16 <xsl:template match="address">EMail Address: <xsl:value-of select="." />
17 </xsl:template>
18 </xsl:stylesheet>
The xsl:foreach
tag loops on each tag containing one of the file names (/mail_list/file in this case). The xsl:apply-templates
tag does a select on each file using this statement: document(@name)/entry. This statement opens each file and selects the the entry element from the document root for each file. Any XSLT templates are applied to entry and any of its subelements.
Here is what the output from the program looks like.
Name: John Doe EMail Address: jdoe@example1.com Name: Jane Smith EMail Address: jane@example2.com Name: Jill Jones EMail Address: jill@example3.com