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

Tip: Batch Processing XML with XSLT 2.0

By Jack Herrington
2005-05-04


Test It Out

To test this system, I'm using a sample set of test results in three different XML files: test1.xml, test2.xml, and test3.xml. I want to read them all and create corresponding HTML files for each one. Listing 3 shows one such sample test file.

Listing 3. A test file in XML

<?xml version="1.0" encoding="UTF-8"?>

<testrun run="test1">
<test name="foo" pass="true" />
<test name="bar" pass="true" />
<test name="baz" pass="true" />
</testrun>
The first step is to run HXDLG to get the directory listing in XML. This directory listing contains the URLs of the test files and will be the input to the XSL stylesheet.

Reading from multiple files in XSL
For the first pass, I'm just going to read the files and print the test name (see Listing 4). Doing so ensures that I can parse the directory structure and read the target files.

Listing 4. Printout of test names

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text" indent="no"/>

<xsl:template match="/">
<xsl:for-each select="//*:file">
<xsl:variable select="document(@url)" name="contents" />
<xsl:value-of select="$contents/testrun/@run" /><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
The first thing the XSLT engine does with the directory listing, which is the input, is match it to the template. The template then iterates through each file tag using the for-each XSL tag. The fun stuff happens when I use the XSL variable tag to call document, which reads the contents of the specified XML file into the variable. XSL makes reading XML documents a snap.

Now, with the contents of the XML test file in hand, I use the value-of tag to print the name of the test run followed by a carriage return with the xsl:text tag (see Listing 5).

Listing 5. The output of the first XSL template

test1

test2
test3
The output shows three files and three tests. So, the tool's working so far. Now all I have to do is build the HTML for each test result. To do that, I'm going to use the xsl:result-document tag, a new feature of XSLT 2.0. (That's why in Listing 6, the version attribute on the stylesheet tag has been bumped to 2.0.)

Listing 6. The stylesheet that creates the HTML files

<?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" indent="no"/>
<xsl:output method="html" indent="yes" name="html"/>

<xsl:template match="/">
<xsl:for-each select="//*:file">
<xsl:variable select="document(@url)" name="contents" />
<xsl:variable select="replace(@url,'[.]xml','.html')"
name="newfile" />
Creating <xsl:value-of select="$newfile" />
<xsl:result-document href="{$newfile}" format="html">
<html><body>
Test run: <xsl:value-of select="$contents/testrun/@run" />
</body></html>
</xsl:result-document>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
Where I used to print the test run name, I now use a variable tag to build a new file name for the HTML. Using the XPath function replace, I take the original URL and replace the .xml extension with .html to create the new file name.

Next, I print the name of the file to let the user know what I'm creating. This is always a good idea because otherwise you would see nothing and have no idea whether the stylesheet did anything.

After I print the message, I use the xsl:result-document tag to create the new file, with some HTML that gives the name of the test run. One thing to notice here is that I had to use a format statement to specify that the output file should be HTML. If I hadn't done this, the file that I created would be in text format and all the HTML tags would have been ignored.

Tutorial Pages:
» Use Directory Listings in XML to Drive XSLT 2.0 Processing
» HXDLG to the Rescue!
» Test It Out
» 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