xml to html

Mitch

Usenet Archive
Joined
May 10, 2001
Messages
89
Reaction score
0
Hi,

I'm looking for a way to take an xml file and automatically (no human intervention) create an .html file. I see serveral ways that might work, but none that exactly meet my needs. The .xml only contains part of the data so formatting the .xml is not really what I want.

Given that I'm a newbie, excuse any syntax errors.

For example:

<xml>
<record>
<key>bob</key>
<data1>data11</data1>
<data2>data12</data2>
</record>
<record>
<key>key2</key>
<data1>data21</data1>
<data2>data22</data2>
</record>
</xml>

But the .html contains the rest of the page, formatting, additional data and a key that indicates which data from the xml should be used.

<html>
....
Some things to know about Bob is, <key=bob, value=data1> and <key=bob, value=data2>.

which would somehow substitute the values data11 and data12 by keyed lookup.

What are the best techniques to do this? CSS seems to be about formatting as well as every other technique I've seen.

thx.
 

Armel

Usenet Archive
Joined
Apr 26, 2007
Messages
11
Reaction score
0
Mitch said:
Hi,

I'm looking for a way to take an xml file and automatically (no human intervention) create an .html file. I see serveral ways that might work, but none that exactly meet my needs. The .xml only contains part of the data so formatting the .xml is not really what I want. [..] What are the best techniques to do this? CSS seems to be about formatting as well as every other technique I've seen.
XSLT is probably what you are searching for (you can access data from several documents: through 'document' function, and replacing your 'place holder', is easy you need to use your tag/element <place-holder/> and match it with a 'xsl:template'). take some time to read some XSL tutorial and all of that will be clear :)

HIH
Armel
 

C. M. Sperberg-McQueen

Usenet Archive
Joined
Jul 2, 2003
Messages
95
Reaction score
0
Mitch writes:

Hi,

I'm looking for a way to take an xml file and automatically (no human intervention) create an .html file. I see serveral ways that might work, but none that exactly meet my needs. The .xml only contains part of the data so formatting the .xml is not really what I want.

Well, it depends to some extent on what one understands "formatting" to involve. XSLT was designed for this kind of task, and while at a first approximation one can say that it can be used to "format the XML", it was designed by people for whom "formatting" was a fairly expansive term. So with XSLT it's certainly possile to integrate data from multiple sources.

Given that I'm a newbie, excuse any syntax errors.

OK, and given that you're a newbie, I'll try to point some of them out more gently than some software may.

For example:

<xml>
<record>
<key>bob</key>
<data1>data11</data1>
<data2>data12</data2>
</record>
<record>
<key>key2</key>
<data1>data21</data1>
<data2>data22</data2>
...

Names beginning with the letters "XML" (or "xml" or any mixed-case combination) are reserved, so your root element will need to be renamed. But OK, so far so good.

But the .html contains the rest of the page, formatting, additional data and a key that indicates which data from the xml should be used.

<html>
...
Some things to know about Bob is, <key=bob, value=data1> and <key=bob, value=data2>.

which would somehow substitute the values data11 and data12 by keyed lookup.

What are the best techniques to do this? CSS seems to be about formatting as well as every other technique I've seen.

CSS is indeed focused on attaching formatting properties to an existing tree of data, not to transforming that tree or reflecting data from one place to another. This makes CSS simpler to process, in some ways, than languages like XSLT, which can perform arbitrary transformations on data.

Like others who have responded, I think what you want to do is learn a bit about XSLT.

The structure you describe is very similar to an example given in the XSLT 1.0 spec in section 2.1 "Literal Result Element as Stylesheet". If we modify your data slightly, to get rid of the illegal name and to put in some very slightly more interesting data about Bob, we might have:

<mydata>
<record>
<key>bob</key>
<name>Bob</name>
<fullname>Robert J. Stein</fullname>
<hair>brown</hair>
<music>likes to listen to James Brown</music>
</record>
<record>
<key>key2</key>
<data1>data21</data1>
<data2>data22</data2>
</record>
</mydata>

The following stylesheet (yes, it's a stylesheet within the meaning of the spec) can be used to process the XML just given:

<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/xhtml1/strict">
<head>
<title>Some facts about some people</title>
</head>
<body>
<p>
Some things to know about
<xsl:value-of select="/mydata/record[key='bob']/name"/> (<xsl:value-of select="/mydata/record[key='bob']/fullname"/> to the bureaucracy):
his hair is <xsl:value-of select="/mydata/record[key='bob']/hair"/>, and he <xsl:value-of select="/mydata/record[key='bob']/music"/>. </p></body> </html>

If we invoke an XSLT processor (on my system I typed
"xsltproc template2.html data2.xml") it produces the output I think you had in mind (I've reformatted a bit to make long lines shorter):

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/TR/xhtml1/strict">
<head><title>Some facts about some people</title></head> <body><p> Some things to know about Bob (Robert J. Stein to the bureaucracy):
his hair is brown,
and he likes to listen to James Brown.
</p></body></html>

My strong instinct is that in the long run you will be better off writing stylesheets in the usual way, with xsl:stylesheet and not html as the root element. But the technique just shown was included in the spec for a reason, and if it suits your needs, there's no reason you shouldn't use it.
 

Peter Flynn

Usenet Archive
Joined
Jan 2, 2000
Messages
1,813
Reaction score
0
C. M. Sperberg-McQueen wrote:
[...]
CSS is indeed focused on attaching formatting properties to an existing tree of data, not to transforming that tree or reflecting data from one place to another. This makes CSS simpler to process, in some ways, than languages like XSLT, which can perform arbitrary transformations on data.

One other thing to be aware of is that CSS is unevenly supported by web browsers, so many things which you might expect to work, won't.

///Peter
 

John Redmond

Usenet Archive
Joined
Jun 27, 2006
Messages
8
Reaction score
0
Mitch:

I sympathise with what you are trying to do, but you have to make a basic decision about what sort of XML file you are setting up in the first place, in particular:

1. Which tags you are using? XHTML tags are the most obvious;
2. Whether you are structuring your XML in the same way as in XHTML. The ORDER is the real issue.

If you satisfy these two requirements, I can help you to do it very easily. Look at my website at www.limpidsoft.com and more particularly www.limpidsoft.com/lexxia.

But there is a number 3: Lexxia is not yet ready for Windows. For Linux, I updated the site only yesterday -- and it is ready to go.

Final words: if you cannot satisfy 1. and 2., you really DO need XSLT. Lexxia helps there, too, and makes it easy to write stylesheets. AND, believe it or not, Lexxia makes it even easier to build LaTeX and PDF (see www.limpidsoft.com/latex). And, of course, it's free.

I hope this helps.

John Redmond
Sydney, Australia.
 

John Redmond

Usenet Archive
Joined
Jun 27, 2006
Messages
8
Reaction score
0
Mitch:

I sympathise with what you are trying to do, but you have to make a basic decision about what sort of XML file you are setting up in the first place, in particular:

1. Which tags you are using? XHTML tags are the most obvious;
2. Whether you are structuring your XML in the same way as in XHTML. The ORDER is the real issue. From your example, it appears that you have a database-like XML source. In that event, you would have to use inline XSLT, whether it is in Lexxia or anywhere else.

[There is a bizarre twist, though: you CAN wrap any old (valid) XML document into XHTML, and you will see every bit of text in a browser, but there will, of course, be no formatting. If, however, you write a CSS stylesheet for each of your tags, it looks surprisingly good. It allows you to suppress specific tags, but no change in sequence. It's all bound to be browser-dependent, though.]

If you satisfy these two requirements, I can help you to do it very easily. Look at my website at www.limpidsoft.com and more particularly www.limpidsoft.com/lexxia.

But there is a number 3: Lexxia is not yet ready for Windows. For Linux, I updated the site only yesterday -- and it is ready to go.

Final words: if you cannot satisfy 1. and 2., you really DO need XSLT to do it cleanly. Lexxia helps there, too, and makes it easy to write stylesheets. AND, believe it or not, Lexxia makes it even easier to build LaTeX and PDF (see www.limpidsoft.com/latex). And, of course, it's free.

I hope this helps.

John Redmond
Sydney, Australia.
 

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
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top