Thanks,
Aaron
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> </xsl:text>
<xsl:apply-templates select="recordNode"/>
</xsl:template>
<xsl:template match="recordNode" xml:space="default">
<xsl:apply-templates select="*"/>
<xsl:text> </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...