By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Convert CSV To html via XSLT

P: n/a
Hi,

Just wondering if anyone out there knows if it is possible to convert
a CSV to xml using XSLT?

I've seen a lot of examples of xml to CSV, but is it possible to go
back the other way?

I don't want to have to use some external program or script to parse
the csv first if possible

Chris

May 24 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On May 24, 11:29 am, Chris <chrisjohnd...@gmail.comwrote:
Hi,

Just wondering if anyone out there knows if it is possible to convert
a CSV to xml using XSLT?

I've seen a lot of examples of xml to CSV, but is it possible to go
back the other way?

I don't want to have to use some external program or script to parse
the csv first if possible

Chris
Sorry just seen this a post about this already, feel free to remove
this one :) :) :)

May 24 '07 #2

P: n/a
On 24 May, 11:29, Chris <chrisjohnd...@gmail.comwrote:
Just wondering if anyone out there knows if it is possible to convert
a CSV to xml using XSLT?
Yes, but you need to pre-process into XML first.

Usually a simple one-liner in sed (commas to tags) and adding a header/
footer root element is adequate.

May 25 '07 #3

P: n/a
>Just wondering if anyone out there knows if it is possible to convert
>a CSV to xml using XSLT?

Yes, but you need to pre-process into XML first.
Which is another way of saying "No". :-)
Usually a simple one-liner in sed (commas to tags) and adding a header/
footer root element is adequate.
Yes, but then you've done your CVS --XML in sed. Of course once
it's in well-formed XML, you can use XSLT in all sorts of ways.

But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.

-- Scott

May 25 '07 #4

P: n/a
Scott Sauyet wrote:
But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.
Never say never:-)

http://dpcarlisle.blogspot.com/2007/...e-updated.html

XSLT2 can input text files, and has regex support which means you can
parse all sorts of things, HTML as in the example above, JSON
here

http://www.biglist.com/lists/xsl-lis.../msg00253.html

etc

David
May 25 '07 #5

P: n/a
David Carlisle wrote:
Scott Sauyet wrote:
>But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.

Never say never:-) [ ... ]

XSLT2 can input text files, and has regex support which means you can
parse all sorts of things, [ ... ]
This is true, of course, and it'll teach me to open my mouth without
all the facts! :-)

Still, I'm a little uncomfortable calling this a conversion of a CSV
using XSLT. I really think that what we say we're converting really
should be the input document, which still needs to be XML (as far as I
know, not really having followed XSLT2! :-) )

The fact that we could import a separate text document and process it
instead of the actual input document is akin to saying that we can
extract square roots with XSLT or solve the Towers of Hanoi with
XSLT. In fact, XSLT is Turing complete:

http://www.unidex.com/turing/utm.htm

But I still wouldn't suggest using XSLT to write your next chess-
playing program!

-- Scott

May 29 '07 #6

P: n/a
Scott Sauyet schrieb:
But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.
The javax.xml.transform.Transformer has a method

transform(Source xmlSource, Result outputTarget)

The javax.xml.transform.Source can e.g. be a
javax.xml.transform.dom.DOMSource, a wrapper around a DOM node. The node
can also be an HTML DOM node.

--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
May 29 '07 #7

P: n/a
On May 29, 9:50 am, Johannes Koch <k...@w3development.dewrote:
Scott Sauyet schrieb:
>But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.

The javax.xml.transform.Transformer has a method
transform(Source xmlSource, Result outputTarget)
The OP asked, though,

Just wondering if anyone out there knows if it is possible to
convert a CSV to xml using XSLT? [ ... ] I don't want to have
to use some external program or script to parse the csv first
if possible

Java is my main coding language, and I do a lot of transforms in it.
But the question was really about XSLT and its capabilities. Although
David Carlisle pointed out that there are techniques that could be
used to import the CSV as text and parse that with some well-written
templates and RegExes, I still believe the best answer to the question
is that XSLT needs XML for input.

-- Scott

May 29 '07 #8

P: n/a
On 25 May, 16:41, Scott Sauyet <Scott.Sau...@gmail.comwrote:
Yes, but you need to pre-process into XML first.

Which is another way of saying "No". :-)
Depends which of the OP's questions we're answering. The subject says
"HTML", the body says "XML".

If we're after XML, then XSLT isn't usually going to be the simplest
route to get there from CSV. Although if it's one tool you're familiar
with (maybe you need to target both), then it might have much to
recommend it.

If we're after HTML, then typically this involves a lot of additional
and semi-trivial transform work on top of a mere syntactic conversion
to XML. That's the sort of task where XSLT is better suited than most
other tools.
It can also be fiendishly difficult to convert CSV to XML, in the
limit case of Arabic or Klingon encoded characters and output from
perverse old steam-mainframes in pseudo-EBCDIC. Just that much at the
character can _sometimes_ turn out to be a nightmare on its own, and
is often enough to break many amateur-coded tools.

May 29 '07 #9

P: n/a
Ixa
possible to convert a CSV to xml using XSLT?

Depends on your CSV and XSLT processor, and even then it can be seen as
some sort of a hack that should be avoided. CSV isn't really the format
that is supposed to be transformed by XSLT, there are better tools for
that.

You can do this hack by "faking" CSV as HTML by simply telling the XSLT
processor that input is HTML. The processor may have this option and
may (should) additionally support the HTML tag optionality, and thus
may interpret the whole CSV file as one HTML paragraph.

For example, using 'xsltproc':

---8<---8<---
$ cat test.csv
1,2,3,4

$ cat csv-mod.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

$ xsltproc | grep "\--html"
--html: the input document is(are) an HTML file(s)

$ xsltproc --html csv-mod.xsl test.csv
<html><body><p>1,2,3,4
</p></body></html>
---8<---8<---

Once you have the CSV interpreted as HTML, it is just a matter of
converting or stripping the HTML elements and processing the CSV as a
string by some recursive template.

In the end, I guess, it is just a matter of software to provide input
file for the XSLT as a tree, regardless of the original file format
whether it is XML or SGML (HTML), CSV or just text/plain. The software
could read CSV directly and provide input as a one text node or full
tree in some custom predefined tabular structure (like XML Exchange
Table Model DTD), but AFAIK there isn't any such software available
(for a reason).

--
Ixa

May 29 '07 #10

P: n/a
Scott Sauyet wrote:
I still believe the best answer to the question
is that XSLT needs XML for input.
That was true of xslt1 but it explictly isn't true of xslt2, there is no
requirement to supply an xml input file for xslt2 (as you can instead
specify a named template to initiate processing, which need not have a
context node at all) and since xslt2 has regular expression support you
can process text files and in-memory strings.

That said, your basic point is correct though, that in going from csv to
*ml you basically need a text editing system, and while xslt2 has some
facilities in that area, it still isn't perl, and probably isn't the
language of choice, unless this is just a small part of a larger xslt
based project, in which case the xslt2 facilities which allow such
transformations without having to move to mixed-language extensions
are quite useful.

David

--
http://dpcarlisle.blogspot.com
May 29 '07 #11

P: n/a
I really think that what we say we're converting really
should be the input document, which still needs to be XML (as far as I
know, not really having followed XSLT2! :-) )
In XSLT 2.0 it is not necessary to have an input xml document and XSLT 2.0
can be used to read any text file (not only xml) using the standard function
unparsed-text().

In both cases quoted by David Carlisle XSLT is used to read and process
non-xml text.
The fact that we could import a separate text document and process it
instead of the actual input document is akin to saying that we can
extract square roots with XSLT or solve the Towers of Hanoi with
XSLT. In fact, XSLT is Turing complete:
You'll surprize nobody that sqrt() is implemented entirely in XSLT. In fact
this was done more than 5 years ago.

The FXSL library offers comprehensive, pure XSLT implementation of most of
the fundamental mathematical functions, such as powers, logarithms,
trigonometric and hyperbolic trigonometric functions, inverse trigonometric,
inverse hyper-trigonometric functions, finding the zeroes of any continuous
function with one real argument (the roots of the equation f(x) = 0 ),
generation of random numbers, numerical differentiation, numerical
integration, Fibonacci numbers, prime numbers and primality checking, ...,
ets.

So, this is much more than just general theoretic considerations -- we have
these functions implemented in XSLT and we have been using them for quite a
long time.

Using the LR Parsing Framework of FXSL it is now easy and straightforward to
produce a parser for any LR(1) language -- this is how JSON is processed
entirely in XSLT.

Do read more about FXSL here:

http://www.idealliance.org/papers/ex...ovatchev01.pdf

and here:

http://fxsl.sourceforge.net/
Cheers,
Dimitre Novatchev.

"Scott Sauyet" <Sc**********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
David Carlisle wrote:
>Scott Sauyet wrote:
>>But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.

Never say never:-) [ ... ]

XSLT2 can input text files, and has regex support which means you can
parse all sorts of things, [ ... ]

This is true, of course, and it'll teach me to open my mouth without
all the facts! :-)

Still, I'm a little uncomfortable calling this a conversion of a CSV
using XSLT. I really think that what we say we're converting really
should be the input document, which still needs to be XML (as far as I
know, not really having followed XSLT2! :-) )

The fact that we could import a separate text document and process it
instead of the actual input document is akin to saying that we can
extract square roots with XSLT or solve the Towers of Hanoi with
XSLT. In fact, XSLT is Turing complete:

http://www.unidex.com/turing/utm.htm

But I still wouldn't suggest using XSLT to write your next chess-
playing program!

-- Scott

Jun 2 '07 #12

P: n/a
Andrew Welch recently posted the XSLT solution of exactly this problem:

http://ajwelch.blogspot.com/2007/02/...n-xslt-20.html
Cheers,
Dimitre Novatchev
"Scott Sauyet" <Sc**********@gmail.comwrote in message
news:11*********************@g4g2000hsf.googlegrou ps.com...
On May 29, 9:50 am, Johannes Koch <k...@w3development.dewrote:
>Scott Sauyet schrieb:
>>But XSLT will only work on XML documents. It won't even work on older
HTML that doesn't conform to XML standards.

The javax.xml.transform.Transformer has a method
transform(Source xmlSource, Result outputTarget)

The OP asked, though,

Just wondering if anyone out there knows if it is possible to
convert a CSV to xml using XSLT? [ ... ] I don't want to have
to use some external program or script to parse the csv first
if possible

Java is my main coding language, and I do a lot of transforms in it.
But the question was really about XSLT and its capabilities. Although
David Carlisle pointed out that there are techniques that could be
used to import the CSV as text and parse that with some well-written
templates and RegExes, I still believe the best answer to the question
is that XSLT needs XML for input.

-- Scott

Jun 2 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.