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(_at_)gmail(_dot_)com> 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
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--