An XSLT to turn XML into CSV
Today I needed to turn an XML file into a CSV file. I was sure someone would have solved this problem before, but I could not find an appropriate XSLT. The problem can be seperated into two subproblems: one is 'flattening' the XML, by which I mean turning it from
XML:
into
XML:
considering I am interested in converting each 'element' into a CSV line.
The 'namespaced' element names are required, because they serve as the CSV column headers and they are required to be unique (which, for our case, is guaranteed by this approach).
The other subproblem is converting XML to CSV, of which the main challenge was making sure the last element is not followed by a comma.
In the end, I came up with the templates below to print the values of the 'childless' elements.
XML:
The upper template applies the lower template to the child nodes of element nodes called 'element', one 'element' at a time. The lower template determines whether the node has any child elements. If it doesn't, it prints the node. Otherwise, it recursively applies this template to the child nodes that were present. Finally, a comma is placed for each element that isn't the last in the node-set. What's a bit tricky here is that no comma is placed for the last grandchild of an 'element', so you might expect those to be missing, but that one is provided by the comma after the element itself.
XML:
1 | <root>
|
into
XML:
1 | <root>
|
considering I am interested in converting each 'element' into a CSV line.
The 'namespaced' element names are required, because they serve as the CSV column headers and they are required to be unique (which, for our case, is guaranteed by this approach).
The other subproblem is converting XML to CSV, of which the main challenge was making sure the last element is not followed by a comma.
In the end, I came up with the templates below to print the values of the 'childless' elements.
XML:
1 | <xsl:template match="//element">
|
The upper template applies the lower template to the child nodes of element nodes called 'element', one 'element' at a time. The lower template determines whether the node has any child elements. If it doesn't, it prints the node. Otherwise, it recursively applies this template to the child nodes that were present. Finally, a comma is placed for each element that isn't the last in the node-set. What's a bit tricky here is that no comma is placed for the last grandchild of an 'element', so you might expect those to be missing, but that one is provided by the comma after the element itself.
09-'08 Running a Java 1.3.1 JDK on Debian
08-'08 What you could be
Comments
Thanx for the solution, but why does every company have the need to export CSV files....
There are two major reasons:
1) They have been in business for quite a while, started exporting data in CSV format long ago (when XML was not mainstream yet) and still have to support customers that haven't switched (we are talking business to business here) or
2) They are dealing with new customers that wish to receive data in CSV format, because they can not (or do not want to) handle XML
1) They have been in business for quite a while, started exporting data in CSV format long ago (when XML was not mainstream yet) and still have to support customers that haven't switched (we are talking business to business here) or
2) They are dealing with new customers that wish to receive data in CSV format, because they can not (or do not want to) handle XML
I have to convert xml files to .csv format. I can write the xslt code for the transformation but how do I run it?? I am new in all this stuff and I would apreciate any help given. Thank you.
Comments are closed