|
|
Home » XML » Article
Batch processing XML with XSLT 2.0
|
| Article by: | Krunal Patel (3/23/2005) |
|
| Sponsored by: |
Neil Matthews Hypnotherapy, Wilmslow |
| Summary: | What you need is an XML version of the directory listing. Then, you could use that XML file as the single input file to XSLT and process each file using XSLT. It would be wonderful if you could do the directory processing in XSLT directly. Unfortunately, with all the power of XSLT -- and particularly XSLT 2.0 -- the language still doesn't have directory operations. |
|
| Viewed: 5957 times |
Rating (6 votes): |
|
2.5 out of 5 |
|
|
|
Batch processing XML with XSLT 2.0
HXDLG to the rescue!<br>
<br>
While surfing the Web, I found an obscure little Java program called the HTML/XML Directory List Generator (HXDLG) on SourceForge. One of the functions of HXDLG is to create either HTML or XML representations of directory listings. I downloaded the tool and ran the statement in Listing 1 from the command line.<br>
<br>
Listing 1. Code to create an XML directory using HXDLG<br>
The program takes three arguments. The first argument is the output type -- either XML or HTML. The second argument is the directory path. The third argument is the path of the output XML file. The result looks something like the code in Listing 2.
|
Select All Code
|
|
That's some high-end stuff. It has a Document Type Definition (DTD) and uses namespaces, and also has the file names and URLs that you're looking for. With absolute paths, to boot!<br>
<br>
Test it out<br>
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.<br>
<br>
Listing 3. A test file in XML<br>
|
Select All Code
|
|
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.<br>
<br>
Reading from multiple files in XSL<br>
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.<br>
<br>
Listing 4. Printout of test names<br>
|
Select All Code
|
|
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.<br>
<br>
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).<br>
<br>
Listing 5. The output of the first XSL template<br>
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.)<br>
<br>
Listing 6. The stylesheet that creates the HTML files<br>
|
Select All Code
|
|
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.<br>
<br>
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.<br>
<br>
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.<br>
<br>
Summary<br>
Batch processing in XSLT 2.0 is simple if you have a directory listing utility that exports XML and know how to use the xsl:result-document tag to redirect the output of the engine. With these tools in hand, you no longer need fear the directory of XML files that you once might have merged into one mega-file to ease processing.<br>
Resources<br>
<br>
Visit the XSL standards site at the W3C, a handy reference to XSL technologies and standards.<br>
Check out the XPath page at the W3C, which provides version and standard information.<br>
Download Saxon, the popular XSL processor that was used in the creation of this article.<br>
|
|
View highlighted Comments
User Comments on 'Batch processing XML with XSLT 2.0'
|
|
|
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
XML and JavaScript Tutorial by Premshree Pillai
This tutorial shows how we can use XML and client side JavaScript. We will see how we can display the contents of a XML file using JavaScript, accessing child elements, manipulating elements etc. |
 |
Display XML in tree format using Javascript by Premshree Pillai
In this article, I present a XML based client-side JavaScript that reads data from an external XML file, traverses the XML data and displays the same in a tree format. I'll use the XMLDOM ActiveX object built into Microsoft Internet Explorer for the same. |
 |
Pulling Data from Other Web pages with XMLHTTP by Jeff Anderson
Using the little known (as yet) XMLHTTP object, part of the XML DOM Model, you can pull data from other web pages for manipulation or your own purposes. |
 |
Using the XML Data Source Object by Premshree Pillai
The XML Data Source Object (DSO) is a Microsoft ActiveX control built into Microsoft Internet Explorer 4+. Using this object, it is possible to extract content from an external XML file or XML data embedded in the HTML file into a HTML page. In this article, I'll explain how to include the XML DSO, extract content from an external XML file, extract XML data from XML data embedded in the webpage and manipulation using JavaScript |
 |
XML Converter for MySQL database by Rustem
MySQL has no native facilities for dealing with XML. This does not mean we are left out of the XML evolution. RustemSoft XML Converter has ‘MySQL to XML’ support. |
 |
Creating An XML Newsfeed by Jeff Anderson
A full explanation of how to take an XML newsfeed from moreover, amazon affiliates, google or any of the other current feed providers, and turn it into a display page using a little ASP. |
 |
XML Ticker using XML Data Source Object (DSO) by Premshree Pillai
This news ticker shows an example of data binding using the XML DSO which is an ActiveX control built into Microsoft Internet Explorer. |
 |
Amazon Lite Web Service by kokogiak.com
A complete XML web service allowing you to offer complete Amazon searches and disaply results on your site. A fantastic introduction to XML Web Services. |
 |
XML Ticker by Premshree Pillai
This is an XML based JavaScript Ticker that can tick any number of messages. The ticker works with IE only. The ticker reads it's contents, i.e the ticker style, text to be displayed, the link for that particular message from a XML file. |
 |
Batch processing XML with XSLT 2.0 by Krunal Patel
What you need is an XML version of the directory listing. Then, you could use that XML file as the single input file to XSLT and process each file using XSLT. It would be wonderful if you could do the directory processing in XSLT directly. Unfortunately, with all the power of XSLT -- and particularly XSLT 2.0 -- the language still doesn't have directory operations. |
 |
| |