473,499 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert CSV To html via XSLT

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
12 11546
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
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
>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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1854
by: Matt | last post by:
I want to use XML to store a document's configurations. And I can convert to different file format by using XSL. For example, convert to HTML, PDF, or RTF. But the contents are all stored in single...
1
2606
by: Murtaza Tinwala | last post by:
Hello mates, I have to convert an XML document into HTML output through the use of XSLT. I am receiving some text content which contains tab characters. I have to display this content into TD....
1
3085
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
0
1101
by: Phlip | last post by:
XMLians: Suppose I have an Ant script, and I want to convert it to a quicky HTML representation of itself. For esthetics, and eventually for editing and launching events, are there any XSLT...
2
4964
by: Henrik Skak Pedersen | last post by:
Hi, I am trying to convert a very basic WML document to HTML using the word2HTML.xsl stylesheet. But I get an exception when I am trying to convert it: The code looks like this: ...
1
4090
by: Tod Johnson | last post by:
Hello all, Can't figure it out. :( Assume that we have 2 XML document: Document1 (source): <elements> <elementA attribute1="value1" attribute2="value2" ... /> <elementA attribute1="value1"...
2
5266
by: chris | last post by:
Hi there, I create an XML file from a dataset like this: System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(FILENAME); dsUserData1.WriteXml(xmlSW, XmlWriteMode.WriteSchema);...
4
3095
by: elsigh | last post by:
I'm wondering if anyone has any ideas about a way to quickly convert an HTML DOM Node into an XML Document. The goal is that I want to perform XSLT on the Node, which is coded correctly as XHTML....
2
8900
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.