0

I have a .csv file generated and I want to transform it to xml using xslt.

How to do that? I'm not good in xslt. Please, help me.

Thank you.

Here's my .csv file

 CONTRACT_NUMBER,CLIENT_NAME_FULL,STREET_ADDRESS,REGISTRY_NO,DATE_RECEIVED,STATUS,REMARKS
 3300016335,name,"address",1_A,3/26/2014,Received,with signature

Desired xml:

  <root>
    <row>
       <CONTRACT_NUMBER>CONTRACT_NUMBER</CONTRACT_NUMBER>
       <CLIENT_NAME_FULL>CLIENT_NAME_FULL</CLIENT_NAME_FULL>
       <STREET_ADDRESS>STREET_ADDRESS</STREET_ADDRESS>
       <REGISTRY_NO>REGISTRY_NO</REGISTRY_NO>
       <DATE_RECEIVED>DATE_RECEIVED</DATE_RECEIVED>
       <STATUS>STATUS</STATUS>
       <REMARKS>REMARKS</REMARKS>
   </row>
   <row>
       <CONTRACT_NUMBER>345678912</CONTRACT_NUMBER>
       <CLIENT_NAME_FULL>name</CLIENT_NAME_FULL>
       <STREET_ADDRESS>address</STREET_ADDRESS>
       <REGISTRY_NO>122345</REGISTRY_NO>
       <DATE_RECEIVED>10/10/14</DATE_RECEIVED>
       <STATUS>fghdsjkls</STATUS>
       <REMARKS>fdhjgkdfl</REMARKS>
   </row>
  </root>
DanMan
  • 10,986
  • 4
  • 38
  • 58
User014019
  • 1,227
  • 8
  • 33
  • 64
  • XSLT is for XML to XML transformation. – zzzzBov Nov 17 '14 at 05:19
  • 6
    i think xslt is not limited in xml to xml transformation – User014019 Nov 17 '14 at 05:26
  • 1
    Ok, to be clear, XSLT is for XML to * transformation. You don't go from CSV to XML, you'd go from XML to CSV. – zzzzBov Nov 17 '14 at 05:28
  • XSLT is not limited to "XML to XML" transformation. It can transform XML to some other formats too. The *input* is limited to XML, not the output. Can you explain in one sentence why you want to use XSLT for this? – Tomalak Nov 17 '14 at 05:55
  • Have you seen @derek-hunziker 's solution to that question? It looks similar to yours and only relies on XSLT 1.0 features. – molbal Nov 17 '14 at 09:17
  • (Edit: I totally forgot to link it: http://stackoverflow.com/questions/12011669/csv-to-xml-using-xslt-how-to-have-incrementing-column-name ) – molbal Nov 17 '14 at 09:53
  • @molbal You are missing a crucial point: the source document in the other question is XML. The csv is merely the text value of a node in that document. – michael.hor257k Nov 17 '14 at 11:13
  • No I'm 100% sure it's possible without any XML nodes, just raw text. I've seen one snippet online that could select the text without any XML nodes around it and then parse it with XSLT 1.0 functions only. However I cannot reproduce nor find it unfortunately. (I know this sounds super weird, so I'm trying to look that stylesheet up right now) – molbal Nov 17 '14 at 15:39
  • @molbal When you do, please let me know (address your comment to me). – michael.hor257k Nov 18 '14 at 14:09
  • @michael.hor257k I'm sorry I can't find it. It seems I was wrong, what I thought was the solution was only a snippet with some XSLT 2 features I didn't notice (the unparsed text selector). Apologies – molbal Nov 18 '14 at 15:09
  • 2
    Related: http://stackoverflow.com/questions/29295377/xslt-2-0-to-convert-csv-to-xml-format – DanMan Aug 16 '15 at 20:57
  • http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html – ceving Jan 29 '16 at 14:26

2 Answers2

1

What you're asking for (CSV to XML) cannot be done with XSLT.

From wikipedia:

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other objects such as HTML for web pages, plain text or into XSL Formatting Objects which can then be converted to PDF, PostScript and PNG.

XSLT can take a source XML document and transform it into a different document (usually XML, but it could be anything), but XSLT is not an appropriate tool for converting CSV data into a different format.

zzzzBov
  • 167,057
  • 51
  • 314
  • 358
  • 1
    I see but, I have read a blog/article of convert to csv to xml like this http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html – User014019 Nov 17 '14 at 05:48
  • 4
    If you are determined to use the most inappropriate tool there is for any kind of work, then if you are very careful you can, for example, successfully drive in a screw with the rim of a cocktail glass. Just mention to anyone who advises against it that you have seen people do it. In all other cases, take the good advice from people who have screw-driving experience and use a proper tool. – Tomalak Nov 17 '14 at 05:53
  • 5
    @User014019 You have tagged your question XSLT 1.0. The article you have linked to relies on XSLT 2.0. None of the techniques shown there will be available to you if you are using a XSLT 1.0 processor. In any case, XSLT's strength (in any version) is in processing XML; when it comes to parsing text as such (e.g. comma-separated values), it's a very poor tool. – michael.hor257k Nov 17 '14 at 06:33
  • 1
    Of course it can be done with XSLT. It just can't be done with XSLT 1.0. And while its facilities for parsing text may be limited, they are sufficient for the job of parsing CSV, and any limitations in this area are compensated by the fact that it is much better than other tools at generating the XML output. – Michael Kay Nov 17 '14 at 12:31
  • "compensated by the fact that it is much better than other tools at generating XML output" I've never found XSLT to be good at anything. The syntax is awkward and unreadable, making it nigh on incomprehensible. If XSLT were genuinely a good tool at generating XML, more developers would use it. Instead, templating systems like handlebars are preferred due to their ease. – zzzzBov Nov 17 '14 at 17:35
  • As @MichaelKay mentions it is possible but not using XSLT 1.0 There is a similar question using 2.0 https://stackoverflow.com/questions/29295377/xslt-2-0-to-convert-csv-to-xml-format – JRSofty Apr 09 '19 at 11:30
  • @zzzzBov The XSLT syntax for generating XML is XML; it's hard to see how that can be unreadable. – Michael Kay Apr 09 '19 at 14:12
0

You probably could do it with some of the string manipulation functions, but I strongly advise against that. Especially if your schema just basically amounts to:

<row>
    <column /><column /><column /><column />...
</row>
....

You really don't need XSLT for something that simple. Use the right tools for the job, and this is not a job for XSLT, judging by the information given.

DanMan
  • 10,986
  • 4
  • 38
  • 58