Use a single XSLT template to create multiple files
For any reasonably complex data set, you need multiple views to navigate it. Take a QA test system, for example: With a pool of tests and test results, you need to see the data by date, by test category, by individual test, and so on. Each view would be in its own HTML file. So, can you have a single template in XSLT 2.0 build multiple HTML files from the one input data set?
The first version of XSLT was very strict. It had one input and one output (although you could have more than one template file). Version 2 of the standard still restricts you to one input, but the output system is more flexible. Now, you can have multiple output files using the xsl:result-document directive. This new tag has two key attributes, as shown in Table 1.
Table 1. xsl:result-document attributes
| Attribute | Description |
href |
The file name or fully qualified URL of the output file |
format |
The name of the format to use as defined in a corresponding xsl:output directive |
To test this directive, I have one input XML file that includes a set of test results (see Listing 1).
|
This is pretty simple stuff. Within each test run is a set of named tests with the pass flag, which tells you whether the test was successful.
Create a file for each test
The first thing I need to do is create a file for each test result. Listing 2 shows the XSL template.Listing 2. Code to create a file for each test
|
A few things are worth noting, starting right at the top of the file. The version attribute on the stylesheet tag is set to 2.0 so that you can use the xsl:result-document tag. After that, you see that the stylesheet itself is set to text as the output type. This means that if I want the HTML files to have HTML formatting, I need to define a second named format of type html. I use this format in the xsl:result-document tag.
From there, I use an xsl:for-each loop to iterate through the testrun tags. Within each of those tags, I use the variable tag to create a new $filename variable that concatenates the output directory name (output1), the name of the run, and the .html file extension into a single path.
With that in hand, I tell the user what files I’m creating by using the value-of tag with my $filename variable. Then, I open the new document with the xsl:result-document tag and output the HTML. Listing 3 shows the output of the engine when it’s run on the sample data file.
Listing 3. The output of Saxon when run on the sample data file
|
Get better output
It seems somehow counterintuitive that the contents of the xsl:result-document tag are evaluated in the same way as the rest of the template, but they are. And all the variables within the template context are available.
To demonstrate this, I’ve upgraded the code within the xsl:result-document tag to provide more information about the results of the test (see Listing 4).
Listing 4. Better HTML output from the template
|
See Listing 5 for the HTML result of this output.
Listing 5. The upgraded HTML output
|
Create an index
To finish, I need to add an index file that points to all the output test results. To do that, I use another xsl:result-document tag and hard-code the output to go to an index file (see Listing 6).
Listing 6. Code to build the index file
|
This section goes right after the xsl:for-each loop that builds the HTML files for each test case. Listing 7 shows the index file for the example data set.
|
Summary
Using the xsl:result-document directive, you can have a single XSL template output to multiple files from a single data source. This functionality, which was a nonstandard extension in XSLT 1.x, opens a world of opportunity for XSLT template authors.





No Responses to “Tip: Create Multiple Files in XSLT 2.0”