Solved

Escape output in XSLT

Posted on 2010-09-01
5
512 Views
Last Modified: 2012-05-10
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)?
0
Comment
Question by:Blowfelt82
5 Comments
 
LVL 6

Expert Comment

by:apresence
ID: 33575344
Please post a sample of your output, and XSLT file.
0
 

Expert Comment

by:asutorius
ID: 33577693
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
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33584347
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
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 33584401
some references

http://en.wikipedia.org/wiki/Comma-separated_values
gives the explanation about the format

the spec can be found here
http://www.ietf.org/rfc/rfc4180.txt

and the accepted answer for this question has an XSLT that does most of the ground work for escaping
(allthough applied to the specific source XML)
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_26308972.html
0
 

Author Closing Comment

by:Blowfelt82
ID: 33660412
Thanks for the references and the comments, helped me work out how to resolve my problem.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now