Re: xml to html
- From: cmsmcq@xxxxxxx (C. M. Sperberg-McQueen)
- Date: Wed, 04 Feb 2009 08:19:37 -0700
Mitch <bessermt@xxxxxxxxx> 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>
</record>
</xml>
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 Style***".
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 style*** (yes, it's a style*** 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.
--
C. M. Sperberg-McQueen
Black Mesa Technologies LLC
http://www.blackmesatech.com/
.
- Follow-Ups:
- Re: xml to html
- From: Peter Flynn
- Re: xml to html
- From: Johannes Koch
- Re: xml to html
- Prev by Date: Re: Syntax?
- Next by Date: Re: xml to html
- Previous by thread: Syntax?
- Next by thread: Re: xml to html
- Index(es):
Loading