Re: [xsl] reading a .xsv file in xslt

Subject: Re: [xsl] reading a .xsv file in xslt
From: ac <ac@xxxxxxxxxxxxx>
Date: Tue, 02 Feb 2010 23:11:40 -0500
Hi,

Andrew, your code is fine but it seems that, to read lines, the line
<xsl:variable name="lines" select="tokenize($csv, ' ')" as="xs:string+" />
should be more like
<xsl:variable name="lines" select="tokenize($csv, '\r?\n')" as="xs:string+" />
as there could be spaces in the cells, and as the end-of-line would not be recognized anyway.
What do you think?


Also, but purely as a matter of taste and case, since all cell values are strings, I would tend to use attributes, replacing
<elem name="{.}">
<xsl:value-of select="$lineItems[$pos]" />
</elem>
with
<xsl:attribute name="{.}" select="$lineItems[$pos]"/>


Finally, one may also have to handle the case of csv files that do not have an initial header line with all valid QName strings.

I get a lot of csv files (e.g. from the bank) where the first line is a blank line, which can be handled by the template with something like {concat('col', string($pos))} for the attribute/node name, after having parsed the second line to figure out how many columns per row. The case where the file starts directly with a data row probably requires providing that initial header row somehow, either by editing the csv or by providing it as a parameter to the template (e.g. if the parameter is empty, check in the file, otherwise try to use the parameter values (if the value count matches the number of data columns;-)). Note that sometimes I wish that I could shoot the bank for not putting the headers in ..., but to no avail... And it is not just the bank ...

Cheers,
ac



Andrew Welch wrote:
On 2 February 2010 20:34, a kusa <akusa8@xxxxxxxxx> wrote:
Hi

Is it possible to read .csv files in xslt and perform some xslt transformations?

Thanks in advance for your help.

The most popular page on the site:


http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html

Excellent Andrew!

Beautiful code, oh what beautiful code.
Succinct and clear.
No need for VB mode.

Regards

Justin Johansson

Current Thread