Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting an XML file to a comma delimited file format

Status
Not open for further replies.

tbt102

Programmer
Jan 1, 2002
61
US
Hi all,

I'm new to XML so please bear with me.

I have an XML file that I need to convert to a comma delimited file format.
What is the best way to get this done?


Environment
===========
Windows NT 4.0 fixpac 6a
Java 2
And VB 6.0

Thanks in advance for any help.

James
 
you have to associate the xml file with a stylesheet(xsl).
Here is the sample xsl which will generate the csv file.
I have done this with XSQL.

xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;<xsl:eek:utput method=&quot;text&quot;/>
<xsl:template match=&quot;/&quot;>
<xsl:text>Field1,Field2,Field3,Field4,Field5</xsl:text>
<xsl:text>&#xa;</xsl:text>
<xsl:text>Field1,</xsl:text>
<xsl:text>Field2,</xsl:text>
<xsl:text>Field3,</xsl:text>
<xsl:text>Field4,</xsl:text>
<xsl:text>Field5,</xsl:text>
<xsl:for-each select=&quot;page/QUERY/ROW&quot;>
<xsl:value-of select=&quot;Field1&quot;/> <xsl:text>,</xsl:text>
<xsl:value-of select=&quot;Field2&quot;/> <xsl:text>,</xsl:text>
<xsl:value-of select=&quot;Field3&quot;/> <xsl:text>,</xsl:text>
<xsl:value-of select=&quot;Field4&quot;/> <xsl:text>,</xsl:text>
<xsl:value-of select=&quot;Field5&quot;/> <xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Talent is what you possess;
genius is what possesses you
 
How about the reverse ? Converting CSV to XML using an XML stylesheet ? Can this be done ?

Toril
 
yes, but you'd have to wrap the csv in a tag so that it technically becomes an xml file. eg.

<myxml>
c,s,v
1,3,4
4,5,6
</myxml>

it should be quite easy with a bit of thought. if you really want to know how it's done just shout but it should be easier to do this in perl or vbscript.
 

given
<rows>
<row>
<field>
etc

then some variation of below should work for you..

<xsl:template match='/'>
<xsl:apply-templates select='row' />
</xsl:template>

<xsl:template match=&quot;row&quot;>
<!-- quoth each field in the row and end with crlf -->
<xsl:for-each select='*'>
<xsl:text>&quot;</xsl:text>
<xsl:value-of select='.' />
<xsl:text>&quot;</xsl:text>
<xsl:if test=&quot;last() != position()&quot;>
<xsl:text>,<xsl:text>
</xsl:if>
</xsl:for-each>
<!-- newline at end of row -->
<xsl:text>
</xsl:text>
</xsl:template>

csv to xml is much harder. need to use the functions substring(), substring-before(), substring-after() recursively.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top