ramlicious Blogs by Tina & Prabhu

October 28, 2010

XML to CSV using XSL and VBScript

Filed under: Scripting,Visual Studio,XML — Prabhuram @ 7:35 am

We have seen XML Transformation using XSL: A sample before to transform an XML to a HTML using XSLT. This time we will see how to convert an XML to a CSV file.

The output will be a simple tab separated list with carriage return as the delimiter like this:

Empire Burlesque        Bob Dylan       USA
Roja    AR Rehman       India
Eagles  Eagles  USA

We will use the same catalog.xml which we used in our earlier example. And our XSL is going to generate a text instead of html which can done by this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text"/>
	<xsl:strip-space elements="*"/>
	<xsl:param name="separator" select="'&#9;'"/>
	<xsl:param name="line-separator" select="'&#13;&#10;'"/>

	<xsl:template match="/">
		<xsl:for-each select="catalog/cd">
			<xsl:value-of select="title"/>
			<xsl:value-of select="$separator"/>
			<xsl:value-of select="artist"/>
			<xsl:value-of select="$separator"/>
			<xsl:value-of select="country"/>
			<xsl:value-of select="$separator"/>
			<xsl:value-of select="$line-separator"/>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

A simple vbscript will do the trick to transform the xml to a file.

 

The complete code is available here: catalog-csv.zip (1.05 kb)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress