Convert CSV To html via XSLT

C

Chris

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
 
C

Chris

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 :) :) :)
 
A

Andy Dingley

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.
 
S

Scott Sauyet

Just wondering if anyone out there knows if it is possible to convert
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
 
S

Scott Sauyet

David said:
Scott said:
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
 
J

Johannes Koch

Scott said:
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.
 
S

Scott Sauyet

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
 
A

Andy Dingley

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.
 
I

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).
 
D

David Carlisle

Scott said:
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
 
D

Dimitre Novatchev

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/e...o-pdf/2006/Novatchev01/EML2006Novatchev01.pdf

and here:

http://fxsl.sourceforge.net/


Cheers,
Dimitre Novatchev.



Scott Sauyet said:
David said:
Scott said:
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
 
D

Dimitre Novatchev

Andrew Welch recently posted the XSLT solution of exactly this problem:

http://ajwelch.blogspot.com/2007/02/csv-to-xml-converter-in-xslt-20.html


Cheers,
Dimitre Novatchev


Scott Sauyet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top