Link to home
Start Free TrialLog in
Avatar of Blowfelt82
Blowfelt82

asked on

Escape output in XSLT

I have an XSLT stylesheet that converts XML into CSV formatted data. This stylesheet works perfectly except for the fact that if the data contains commas the CSV output obviously gets its columns mixed up. What I want to know is how I can escape these characters, and also what characters I should worry about (is it just commas that can mess me up in CSV formatted files)?
Avatar of apresence
apresence

Please post a sample of your output, and XSLT file.
Do you truly want to escape the commas or do you need them in the CSV? Are the commas vital parts of the data?

I was wondering if this article applies to you, specifically steps 2 and 3 where they recursively step through keeping the commas:
http://www.dpawson.co.uk/xsl/sect2/N1755.html
Avatar of Gertone (Geert Bormans)
I don't see any answers given yet
The reference to Dave Pawson's FAQ list has examples for CSV into XML, not the other way round

CSV is tricky, because a lot of the CSV importers don't support ful CSV standard.
Before going into the XSLT it is worth checking what your CSV importer supports, eg. is it Excel?

The spec for CSV more or less says the following

- if there is a comma in the data, put double quotes around the datafield
- if there are double quotes in the data and you need to escape comma's, make the double quotes double

so if this is your data
<field>a</field><field>b</field><field>c</field>
this would be the csv
a,b,c

if this is your data
<field>a</field><field>b,b</field><field>c</field>
this would be the csv
a,"b,b",c
allthough some importers would expect
a,"b,b",c

if this is your data
<field>a</field><field>b"b</field><field>c</field>
this would be the csv
a,b"b,c

if this is your data
<field>a"a</field><field>b,b</field><field>c</field>
this would be the csv
"a""a","b,b","c"

But you should make an example file first and see how your importer deals with them, because most importers are pretty weak in supporting full CSV spec

I have a question answered recently here where I expand more on CSV. I will try to find it
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Blowfelt82

ASKER

Thanks for the references and the comments, helped me work out how to resolve my problem.