We help IT Professionals succeed at work.

CSV to XML using XSLT

1,528 Views
Last Modified: 2013-11-18
I have a file which is in the following format
1,2,3,4,5,6,7^a^b^c^d^e^f^g^h^i^j I need to convert it to an XML using XSLT. The csv file could change its format sometime later so need to use XSLT other than changing the code, changes in XSL should be enough

Could anyone help please. It is urgent.

YRKS

Comment
Watch Question

Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Since this is .net,
can you use XSLT2, with the .net version of saxon (www.saxonica.com)
of do you need xslt1 (which will be a lot trickier)

can you also show the result you expect from this?

can you wrap this in a root tag, like this?
<root>1,2,3,4,5,6,7^a^b^c^d^e^f^g^h^i^j</root>

XSLT requires wellformed XML as its source format
so you need to take precautions to escape occasional "&" into "&amp;", same with "<" and ">"
so preferably you use some dom methods to xml-ify the text string

I know I strongly advised against using XSLT for this task in your previous question
You must have good reasons to ignore that recommendation, so I hope you understand all the consequences
I really hope you can embed Saxon in your application and use XSLT2

cheers

Geert

Author

Commented:
Thanks for your prompt reply.  I did use the other solution but just had a discussion with the group and they all want something that will run such that if the XML changes then we donot have to come back and recompile the program.

I did check the saxon site and realised that I need to purchase the products. There is no trial download. If the trial download works I could ask the company to go ahead and buy the software. As I cannot try out the software and see what it does cannot really recommend.

YRKS

Author

Commented:
Did find an evaluation copy
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Hi YRKS,

you have misread the buying part
There are two Saxon Products
Saxon8B can be used free and is all you need
Saxon8SA is the schema aware alternative, you don't need it and even if you did, you can have an evaluation copy

look for Saxon8B, you can download and use that for free

cheers

Geert

Author

Commented:
I have a further question before I invest to much time in this
my csv file 1,2,3,4,5,6,7^a^b^c^d^e^f^g^h^i^j

needs to be converted into an xml like
<a>
   <b>
         <c>1</c>
         <d>c</d>
         <e>2</e>
   </b>
  <f>
       <g>7</g>
     <h>i</h>
  </f>
</a>

So the order of the content in csv and xml output do not match. Is this possible to do with Saxon8B
Thanks for all your help
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Order is not important,
as long as there is a certain logic you can implement, you can do this

Author

Commented:
I will be having mapping information ie  g element corresponds to the 7the position of the above csv(it is actually not even a csv first 7 positions are comma separated everything else is ^ separated.)  element h corresponds to the 18th position in the csv. Is that information enough. I will try out the saxon code sometime in the next few days.
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
that would be enough
If you could handwrite an XML coming from the above strin, that would be better

Author

Commented:
"If  you could handwrite an XML coming from the above strin, that would be better"

Are you suggesting that using the method mrihm: suggested is a better way to go.

 'Write the tag that represents the line item
   tw.WriteStartElement("element_name")

Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
no,no I meant given this string
1,2,3,4,5,6,7^a^b^c^d^e^f^g^h^i^j

show me the XML you want from it, plus why you did what,
All I need is some logic so I can develop the XSLT
I can't invent the rules myself

Author

Commented:
Here is the xml I have it posted in the 3rd or 4th comment up here. You must have just missed it, thats the reason I got confused to. Thanks for all your help  and patience.


my csv file 1,2,3,4,5,6,7^a^b^c^d^e^f^g^h^i^j


needs to be converted into an xml like
<a>
   <b>
         <c>1</c>
         <d>c</d>
         <e>2</e>
   </b>
  <f>
       <g>7</g>
     <h>i</h>
  </f>
</a>
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
no, no, I did not miss it, but what are the rules?
How do I know thatc,d,e need to be nested in b and g,h need to b enested in f
and why is c put as a value inside d?

Author

Commented:
the xml and csv are files which are tranferred from one company to another. Each company has aformat it needs the data in company 1 needs it in an XML format and company 2 needs it in a csv format.  

This is a standard format of xml that is already defined like the one I showed and I need to construct the given xml from the csv,  the mapping is also predefined. There are no rules as such. both the csv and XML are messages which need to be send from one system to another in a particular format .

The client wants to have the flexibility of adding new feilds with minimum impact to the system and wants the entire process to be as generic as possible.
Thanks again for all your help and patience.
YRKS
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
well, based on this information, I can only hardcode the XML
it won't be too flexible
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Geert,

I will accept this as solution and then go create anohther question as it relates to setting up the environment correctly.
YRKS
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.