Parsing Comma-Separated Values
By Doug Tidwell2005-05-18
Converting the generated XML
Now that I had this XML-tagged data, I needed to write an XSLT style sheet that would convert the XML above into a more useful (and understandable) tag set. You can view the style sheet listing in a separate window, and you can also download it and view it locally.
To convert the generated XML document using the rules in the style sheet and then write the output to the file called employees.xml, I typed:
|
If you want to run the transform code, you'll need the Xerces XML parser and version 1.0.0 of the Xalan style sheet processor (see Resources), both of which are free and available at xml.apache.org. The resulting XML looks like Listing 2. (You can also download employees.xml and employees.dtd.)
Listing 2. XML output of the document converted using csv-stylesheet.xsl |
Figure 1. Excerpt of the HTML table converted from the CSV data via the style sheet employee-table.xsl
| Employee | Sex | Serial Number | Department | Job Title | Date of Hire | Years of Education |
| Phone | Date of Birth | Salary | Bonus | Commission | Total Compensation | |
| ADAMSON, BRUCE | M | 000150 | D11 | DESIGNER | 02/12/1972 | 16 |
| 4510 | 05/17/1947 | $25,280.00 | $500.00 | $2,022.00 | $27,802.00 | |
| BROWN, DAVID | M | 000200 | D11 | DESIGNER | 03/03/1966 | 16 |
| 4501 | 05/29/1941 | $27,740.00 | $600.00 | $2,217.00 | $30,557.00 | |
| GEYER, JOHN B. | M | 000050 | E01 | MANAGER | 08/17/1949 | 16 |
| 6789 | 09/15/1925 | $40,175.00 | $800.00 | $3,214.00 | $44,189.00 | |
| GOUNOT, JASON R. | M | 000340 | E21 | FIELDREP | 05/05/1947 | 16 |
| 5698 | 05/17/1926 | $23,840.00 | $500.00 | $1,907.00 | $26,247.00 | |
| HAAS, CHRISTINE I. | F | 000010 | A00 | PRES | 01/01/1965 | 18 |
| 3978 | 08/24/1933 | $52,750.00 | $1,000.00 | $4,220.00 | $57,970.00 |
You can view the entire table in a separate window, and also view the HTML source.
I hope this helps. I've certainly learned some things along the way, and I hope you have too.
Tutorial Pages:
» Getting the data
» Parsing the comma-separated value (CSV) file
» Converting the generated XML
» Resources
First published by IBM DeveloperWorks
