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

2019-04-17 16:05发布

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>

2条回答
够拽才男人
2楼-- · 2019-04-17 16:26

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-17 16:36

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.

查看更多
登录 后发表回答