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

Parsing Comma-Separated Values

By Doug Tidwell
2005-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:

java org.apache.xalan.xslt.Process -in output.xml -xsl csv-stylesheet.xsl 

-out employees.xml

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


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>
<employee sex="F">
<serial_number>000010</serial_number>
<name>
<first_name>CHRISTINE</first_name>
<middle_initial>I</middle_initial>
<last_name>HAAS</last_name>
</name>
<department>A00</department>
<phone>3978</phone>
<date_of_hire year="1965" month="01" day="01"/>
<job_title>PRES</job_title>
<years_of_education>18</years_of_education>
<date_of_birth year="1933" month="08" day="24"/>
<salary>52750</salary>
<bonus>1000</bonus>
<commission>4220</commission>
</employee>
<employee sex="M">
...
</employee>
</employees>

Figure 1. Excerpt of the HTML table converted from the CSV data via the style sheet employee-table.xsl
EmployeeSexSerial NumberDepartmentJob TitleDate of HireYears of Education
PhoneDate of BirthSalaryBonusCommissionTotal Compensation
ADAMSON, BRUCEM000150D11DESIGNER02/12/197216
451005/17/1947$25,280.00$500.00$2,022.00$27,802.00
BROWN, DAVIDM000200D11DESIGNER03/03/196616
450105/29/1941$27,740.00$600.00$2,217.00$30,557.00
GEYER, JOHN B.M000050E01MANAGER08/17/194916
678909/15/1925$40,175.00$800.00$3,214.00$44,189.00
GOUNOT, JASON R.M000340E21FIELDREP05/05/194716
569805/17/1926$23,840.00$500.00$1,907.00$26,247.00
HAAS, CHRISTINE I.F000010A00PRES01/01/196518
397808/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


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

Ask A Question
characters left.