Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xml to html

From: cmsmcq@---.--- (-. -. ----------------)
To: NULL
Date: 2/4/2009 8:20:00 AM
Mitch <bessermt@g...> 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 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.  

-- 
C. M. Sperberg-McQueen
Black Mesa Technologies LLC
http://www.blackmesatech.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent