Solved

Escape output in XSLT

Posted on 2010-09-01
5
570 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

729 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