Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML/XSLT -- < CSV (Excel) HELP!

0 views
Skip to first unread message

Aaron Ghadiyali

unread,
Apr 23, 2003, 11:18:44 AM4/23/03
to
Hello,
I cannot for the life of me find a good resource online
to describe how to use VB6.0 and convert a XML/XSLT sheet
I have to be easily exported to a .CSV file for use in the
program "Excel". Does anyone know? I would appreciate it.

Thanks,
Aaron

Mike Sharp

unread,
Apr 23, 2003, 12:59:29 PM4/23/03
to
If you mean taking an XML file and using XSLT, creating a CSV file, that
shouldn't be too hard.

In my two templates:
<xsl:template match="*">
and
<xsl:template match="*" mode="fieldnames">
in the example below, I couldn't think of a way to suppress whitespace in
the output, so I simple removed all whitespace from the templates, which
made them a bit hard to read. So I added some comments to provide fake word
wrapping. Hopefully someone can point out a more elegant method of
stripping whitespace in my two templates.

Anyway, if your XML is relatively flat, that makes it easier. If not, then
you'll have to write some extra templates, but if your XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xml2csv.xslt"?>
<root>
<recordNode>
<field1>a</field1>
<field2>b</field2>
<field3>c</field3>
<field4>d</field4>
</recordNode>
<recordNode>
<field1>f</field1>
<field2>h</field2>
<field3>i</field3>
<field4>j</field4>
</recordNode>
</root>

And your XSLT looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="root">
<xsl:apply-templates select="recordNode[1]/*" mode="fieldnames"/>
<xsl:text>&#13;</xsl:text>
<xsl:apply-templates select="recordNode"/>
</xsl:template>
<xsl:template match="recordNode" xml:space="default">
<xsl:apply-templates select="*"/>
<xsl:text>&#13;</xsl:text>
</xsl:template>
<!-- comments are simply taking the place of whitespace
for readability-->
<xsl:template match="*" mode="fieldnames"><!--
-->"<xsl:value-of select="name()"/>"<!--
--><xsl:if test="position() != last()">,</xsl:if><!--
--></xsl:template>
<xsl:template match="*"><!-- fakespace
-->"<xsl:value-of select="."/>"<!-- fakespace
--><xsl:if test="position() != last()">,</xsl:if><!--
--></xsl:template>
</xsl:stylesheet>


Then you get this output:

"field1","field2","field3","field4"

"a","b","c","d"

"f","h","i","j"

Which imports directly into Excel.

Regards,

Mike Sharp

"Aaron Ghadiyali" <aghad...@atlantaregional.com> wrote in message
news:081d01c309ab$a15bc730$a401...@phx.gbl...

0 new messages