Pantvaidya, Vishwajit schrieb:
Using Xalan Java 2.7.1, I am trying to convert a string in a simple xml to a csv file.
This requirement comes up maybe once a month on various XSL lists.
Since the string in the input xml has commas, my xslt has logic to enclose the entire string in quotes. But the string also has quotes which interfere with these - so I am trying to escape those as well into a CSV acceptable form i.e. by replacing a double quote with 2 double quotes.
CSV is trickier than you might think. That's why I'd *alway* use a CSV library to do the job, like Text::CSV_XS for Perl. (I know you're on Java. Read on.)
For that I am using the recursive logic that Jeni Tennison provided in one of the group posts. But still the xslt does not work.
I'm sure Jeni Tennison has come up with something good. On the other hand, XSL 1.0 is not particularly good at string handling. So if I were you, I'd give one of these packages a try: Super Csv - A free CSV reader/writer http://supercsv.sourceforge.net/ opencsv - an open source csv parser for Java http://opencsv.sourceforge.net/ Don't know how well they work. To my surprise, there seems to be no CSV handling functionality included in Java 6. Or has it escaped my attention? At any rate, set up your XSLT to combine rows for CSV, then do the writing line by line calling back to Java using the extension function mechanism. Works in Perl with LibXSLT, so it should work in Java with Xalan. Michael Ludwig