This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: converting XML to comma-separated value text file


Kevin:

OUTPUT of your XML sample:

Product Line,Daily,Todays Cedits,MTD,Average Daily Sales
Colwell,-6816.2,-206.2,-109294.52,-5464.726
Equipment,7671,-16676,654653.59,32732.6795
Eaglesoft,13510,,112417.91,5620.8955
Gold,,,228.95,11.4475
Parts/Service,286,-976.85,158187.5,7909.375
Sundries,79083.19,-11319.22,1807683.51,90384.1755
Teeth,2159.21,-129.45,40836.17,2041.8085
,109525.6,-29307.72,2883302.15,-144165.1075

STYLESHEET:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:include href="csv-util.xsl" />
	<xsl:output method="text"/>
	<xsl:template match="/">
			<!-- csv header -->
			<xsl:apply-templates select="*/column-header/*/." mode="csv"/>
			<!-- csv data -->
			<xsl:for-each select="*/row">
				<xsl:apply-templates
select="row-header/ae-desc|mv/@uf|row-header/gt-header|gt/mv/@uf" mode="csv"/>
			</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

INCLUDED "csv-util.xsl":
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

	<xsl:variable name="quot">"</xsl:variable>
	<xsl:variable name="quot2">""</xsl:variable>

	<xsl:template name="csv-end-of-field">
		<xsl:if test="position() = last()"><xsl:value-of select="'&#xA;'"/></xsl:if>
<!-- new line -->
		<xsl:if test="position() != last()">,</xsl:if> <!-- comma separator -->
	</xsl:template>

	<xsl:template match="@*|*" mode="csv">
		<xsl:value-of select="."/>
		<xsl:call-template name="csv-end-of-field"/>
	</xsl:template>

	<xsl:template match="(@*|*)[contains(.,',')]" mode="csv">
		<xsl:choose>
			<xsl:when test="contains(.,$quot)">
				<!-- duplicate double quotes
				-->"<xsl:call-template name="replace-string">
					<xsl:with-param name="text" select="."/>
				</xsl:call-template>"<!-- no end-of-line -->
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="concat($quot, ., $quot)"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:call-template name="csv-end-of-field"/>
	</xsl:template>

  <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:choose>
      <xsl:when test="contains($text,$quot)">
        <xsl:value-of select="substring-before($text,$quot)"/>
        <xsl:value-of select="$quot2"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="substring-after($text,$quot)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>


Vladimir Zakon.
------------------( Forwarded letter 1 follows )--------------------
Date: Tue, 19 Jun 2001 14:49:57 -0500
To: XSL-List@lists.mulberrytech.com
From: Kevin.Wilson@pattersondental.com
Sender: owner-xsl-list@lists.mulberrytech.com
Reply-To: xsl-list@lists.mulberrytech.com
Subject: [xsl] converting XML to comma-separated value text file

I've been surfing the web for a couple hours, but can't find a basic
discussion of how to convert XML to a CSV text file.
(My background:  I have modified pre-existing XSL stylesheets to convert
the following type of XML into HTML.)  Thanks!

The  XML looks like this:

<dssgrid ns="A">
<style-info StyleID="YB0450EDB225F11D292DC00C04FEDC18B" DispWidth="-1"
GridPadChar="32" GridColSep="32" GridOverflowColSep="9" NewLine="13"
GridSpace="32"></style-info>

<attr n="1" url="Product+Line">Product Line</attr>

<column-header>
     <upper-left rows="1">Product Line</upper-left>
     <mh n="1" cols="1">Daily</mh>
     <mh n="2" cols="1">Todays Cedits</mh>
     <mh n="3" cols="1">MTD</mh>
     <mh n="4" cols="1">Average Daily Sales</mh>
</column-header>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="CO" idurl="CO">Colwell</ae-desc>
     </row-header>
     <mv n="1" uf="-6816.2">($6,816.20)</mv>
     <mv n="2" uf="-206.2">($206.20)</mv>
     <mv n="3" uf="-109294.52">($109,294.52)</mv>
     <mv n="4" uf="-5464.726">($5,464.73)</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="EQ" idurl="EQ">Equipment</ae-desc>
     </row-header>
     <mv n="1" uf="7671">$7,671.00</mv>
     <mv n="2" uf="-16676">($16,676.00)</mv>
     <mv n="3" uf="654653.59">$654,653.59</mv>
     <mv n="4" uf="32732.6795">$32,732.68</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="ES" idurl="ES">Eaglesoft</ae-desc>
     </row-header>
     <mv n="1" uf="13510">$13,510.00</mv>
     <mv n="2" uf=""></mv>
     <mv n="3" uf="112417.91">$112,417.91</mv>
     <mv n="4" uf="5620.8955">$5,620.90</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="GO" idurl="GO">Gold</ae-desc>
     </row-header>
     <mv n="1" uf=""></mv>
     <mv n="2" uf=""></mv>
     <mv n="3" uf="228.95">$228.95</mv>
     <mv n="4" uf="11.4475">$11.45</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="PS" idurl
="PS">Parts/Service</ae-desc>
     </row-header>
     <mv n="1" uf="286">$286.00</mv>
     <mv n="2" uf="-976.85">($976.85)</mv>
     <mv n="3" uf="158187.5">$158,187.50</mv>
     <mv n="4" uf="7909.375">$7,909.38</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="SU" idurl="SU">Sundries</ae-desc>
     </row-header>
     <mv n="1" uf="79083.19">$79,083</mv>
     <mv n="2" uf="-11319.22">($11,319.22)</mv>
     <mv n="3" uf="1807683.51">$1,807,683.51</mv>
     <mv n="4" uf="90384.1755">$90,384.18</mv>
</row>

<row>
     <row-header>
          <ae-desc n="1" rows="1" idval="TE" idurl="TE">Teeth</ae-desc>
     </row-header>
     <mv n="1" uf="2159.21">$2,159.21</mv>
     <mv n="2" uf="-129.45">($129.45)</mv>
     <mv n="3" uf="40836.17">$40,836.17</mv>
     <mv n="4" uf="2041.8085">$2,041.81</mv>
</row>

<row>
     <row-header>
          <gt-header cols="1"></gt-header>
     </row-header>
     <gt>
          <mv n="1" uf="109525.6">$109,525.60</mv>
     </gt>
     <gt>
          <mv n="2" uf="-29307.72">($29,307.72)</mv>
     </gt>
     <gt>
          <mv n="3" uf="2883302.15">$2,883,302.15</mv>
     </gt>
     <gt>
          <mv n="4" uf="-144165.1075">($144,165.11)</mv>
     </gt>
</row>

</dssgrid>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]