Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Tip: Create Multiple Files in XSLT 2.0

By Jack Herrington
2005-05-16


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


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="text"/>
<xsl:output method="html" indent="yes" name="html"/>

<xsl:template match="/">
<xsl:for-each select="//testrun">
<xsl:variable name="filename"
select="concat('output1/',@run,'.html')" />
<xsl:value-of select="$filename" /> <!-- Creating -->
<xsl:result-document href="{$filename}" format="html">
<html><body>
<xsl:value-of select="@run"/>
</body></html>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

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


Creating output1/test1.html
Creating output1/test2.html
Creating output1/test3.html


Tutorial Pages:
» Use a single XSLT template to create multiple files
» Create a file for each test
» Get better output
» Create an index
» Summary
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business

Ask A Question
characters left.