How to convert .csv file to xml using XSLT 1.0?

2019-04-17 15:36发布

问题:

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>

回答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.



回答2:

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.