...

View Full Version : double line breaks in xslt csv export!!



dulatoag
12-17-2008, 10:08 PM
Hey all:

A very simple xslt file here that I'm using to export FileMaker data into a csv file. Works great, but it puts in two line breaks after each row. So when I pull it into excel, I have values on every other row, with a blank in between. Can't figure it out to save my life. Here's the code:


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fm="http://www.filemaker.com/fmpxmlresult"
exclude-result-prefixes="fm">
<xsl:output method="text" version="1.0" encoding="utf-8" indent="no" />
<xsl:template match="/">
<!-- Header line, inserted only once; if not desired, remove -->
<xsl:text>
All of my column headers here ...
</xsl:text>
<!-- LOOP THROUGH THE RECORDS (as ROW) -->

<xsl:for-each select="fm:FMPXMLRESULT/fm:RESULTSET/fm:ROW">
<!-- LOOP THROUGH THE FIELDS (as COL/DATA) -->
<xsl:for-each select="fm:COL/fm:DATA">
<xsl:variable name="fmdata">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="newData">
<xsl:choose>
<xsl:when test="contains( $fmdata, '&quot;' )">

<xsl:call-template name="escape_quotes">
<xsl:with-param name="string" select="$fmdata" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$fmdata"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>


<xsl:variable name="newval">
<xsl:value-of select="translate($newData, '&#xA;', '')"/>
</xsl:variable>

<xsl:value-of select="concat('&#x22;', $newval, '&#x22;')" />
<xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>
<xsl:variable name="thissheet">
<xsl:value-of select="." />
</xsl:variable>
</xsl:template>

<xsl:template name="escape_quotes">
<xsl:param name="string" />

<xsl:value-of select="substring-before( $string, '&quot;' )" />
<xsl:text>""</xsl:text>
<xsl:variable name="substring_after_first_quote" select="substring-after( $string, '&quot;' )" />
<xsl:choose>
<xsl:when test="not( contains( $substring_after_first_quote, '&quot;' ) )">
<xsl:value-of select="$substring_after_first_quote" />
</xsl:when>
<xsl:otherwise>
<!-- The substring after the first quote contains a quote.
So, we call ourself recursively to escape the quotes
in the substring after the first quote.
-->
<xsl:call-template name="escape_quotes">
<xsl:with-param name="string" select="$substring_after_first_quote"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Any help would be greatly appreciated. Thanks.

KeithLawless
12-24-2008, 05:18 PM
Would it be possible to post the source XML file you are using? Also, what tool are using to perform the transformation?

I loaded your XSLT into XMLSpy, and downloaded a sample XML file from the Filemaker site at http://www.filemaker.com/help/12-Import%20export34.html.

The output was what you are expecting - one line of CSV data per line, no extra line-breaks. The only thing I saw out of whack was an extra line feed at the end of the file, resulting in a blank line at the end of the file. Excel didn't seem to mind having that line there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum