Solved

Escape output in XSLT

Posted on 2010-09-01
5
560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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