[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: csv data to xml

Subject: Re: csv data to xml
From: Olivier XILLO <oxillo@xxxxxxxxxxx>
Date: Sat, 29 Jun 2013 14:18:45 +0200
 Re: csv data to xml
Hello

Try reposting as the snippet was truncated by webmail...

I'm doing some batch processing of text files to xml using ANT.
Instead of having the text file content fed to the XSLT through unparsed-text(), I'm wrapping the text file in a temporay xml file by adding a opening and closing tag and then process the resulting XML file using ANT XSLT task.


Below is an adapted snippet of my ant target :

<target name="csv2xml">

<!-- First, create one text file with opening tag and one text file with closing tag -->
<echo file="headxml" message="&lt;textwrapper>"/>
<echo file="tailxml" message="&lt;/textwrapper>"/>


<!-- Next, copy the CSV file from ${from.dir} to ${to.dir}-->
<copy todir="${to.dir}">
<fileset dir="${from.dir}" includes="**/*.CSV"/>
<!-- mapping the file extension from CSV to XCSV -->
<mapper type="glob" from="*.VMF" to="*.xvmf"/>
<!-- and adding the content of tag files at the beginning and the end of each CSV file -->
<filterchain>
<concatfilter prepend="headxml" append="tailxml"/>
</filterchain>
</copy>


<!-- Tag files are not needed anymore, delete them -->
        <delete file="headxml"/>
        <delete file="tailxml"/>

<!-- Process the XCSV files as standard XML files ... -->
<xslt style="stylesheet.xsl" destdir="${to.dir}" basedir="${to.dir}">
<mapper type="glob" from="*.XCSV" to="*.xml"/>
</xslt>
</target>


In your stylesheet, you just have to process the content of <textwrapper/> as

<xsl:template match="/textwrapper">

   <!-- For example, you can extract each line with -->
   <xsl:variable name="lines" select="tokenize(text(),'&#xa;')">
   ....
</xsl:template>

Some warnings however, depending on the content of your CSV file, you may obtain an invalid XML file (due to special characters, tag opening,...) and then it won't work...

Hope it helps!

Olivier

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.