Forum Moderators: open

Message Too Old, No Replies

XML is Greek to me...

Anyone for souvlaki?

         

ronin

8:59 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the capacity of an editor, I'm supposed to be reviewing some info coded for the web. Fair enough, I said, send it to me in validated XHTML and I'll have a look at it.

They sent it to me. And it was validated. But it wasn't XHTML. It was an XML file with an accompanying DTD.

So I had a look at it in IE. You know what happened.

So I had a look at it in Moz. Same code, but this time with a message: "This XML file does not appear to have any style information associated with it. The document tree is shown below."

Hmmm. Looked through the options to see if I could get Firefox to look at the DTD and use that to make sense of the .xml file. Not a chance.

It appears I am dangerously clueless about .xml.

I have spent the last hour trying to find something that will help... I have considered the possibility of writing a web page which includes a javascript which will reference both the DTD and the .xml file. (I still don't know if this would work, but I can't find any info on it either way).

The best I have come up with is to change to the file extension on the .xml file to .html and then look at it again with the browser. Of course browsers ignore anything that doesn't make sense in .html, so this was enough of a step forward that I could read all the content in the file and most of it wasn't mashed together. But there was precious little formatting and it wasn't obvious what was a heading or what was a list either.

How do I get the browser to reference the DTD so that it knows how to display the .xml file? Or have I got that wrong too?

Any Word macros I could use to get rid of the extraneous tags and convert the .xml into .xhtml?

Yes, I know, I'm really clutching at straws now....

Thanks in advance.

ronin

9:47 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, okay, I found this tutorial over at W3Schools:

http://www.w3schools.com/xml/xml_view.asp [w3schools.com]

However, as it says quite plainly at the beginning:

However, to make XML documents to display like nice web pages, you will have to add some display information.

Eh? I have to add some display information? Isn't there any display information in the DTD...? How can I access that?

ronin

10:03 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh. I think I've found my answer here:

http://academ.hvcc.edu/~kantopet/xhtml/index.php?page=xml+and+xhtml&parent=what+is+xhtml [academ.hvcc.edu]

As follows:

Styling XML

Unlike HTML and XHMTL, XML does not have a default set of style rules built into the browser. Therefore, if you are going to being viewing an XML document in some way, you need a style sheet to format it for its presentation medium.

To create a style sheet for an XML document, you need to create a style rule for every element you want displayed. In addition to the normal styling properties, an important feature of an XML style sheet is that you need to declare the display property for each element you want displayed. In other words, you need to tell the browser to actively display the contents, otherwise it won't know what to do with them.

Beyond the need for more information, you can use CSS for XML in exactly the same way you can use it for XHTML. When including the style rules, unless you want to code for it in the DTD, your best bet is to include an external style sheet using an XML directive. The XML directive to include the style sheet takes the following form and should come right after the version declaration.

<?xml-stylesheet href="CSS URL" type="text/css"?>

I think I'd better go back and ask my colleague if he has a stylesheet for the .xml file.

ronin

10:24 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now it gets interesting! I was wondering why the DTD was necessary, if what was needed to display the XML properly was a stylesheet.

Having now uploaded the .xml file to an online server, I see that without the DTD, the code won't even display. Why not I have no idea.

Alright, bear with me... I'm figuring it out gradually.

athinktank

11:27 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



Howdy ronin....

In short.
A DTD, or an XSD, describes what the xml should look like. i.e. can this element have any children elements or attributes etc. They can used to validate xml or allow a user to build a document from the ground up. It has nothing to do with display.

For the xml to display in a browser, it will show up as a tree, for the most part. one simple way to display style ize it would be to add a reference to an xsl stylesheet, much in the same way you were referencing a css document. xsl is a whole beast onto itself.

I like to think of xml as the data. xsd(dtd) as the allowable format of the data. xsl tp processes the data (in my case often into html) and css formats the colors and fonts etc.

don't know if that helps or not. good luck

ronin

1:57 pm on Jul 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, I see. Thanks for that. It looks like I need to get on to my colleague and request either a .css file or an .xsl file or both.

Conscientious Reject

8:08 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Is it possible to store the content to your entire site on one xml document, and add styles to each element using xsl or css? Basically, I am wondering if we could call specific xml elements in accordance with the page we are creating. This way I could easily change the content of my site, by accessing only one xml document.

ronin

11:46 am on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay my colleague has told me this:

Our xml is too complex for a simple css to handle... Our xml is also exported in such a way that it's very difficult (possibly impossible) to build an xslt for it.

That's not very helpful, is it?

Perhaps they don't realise how utterly clueless I am about .xml.

I am afraid that this is going to reveal me as still a total xml dunce after a week and a half of trying and failing to understand it.

But please can someone explain to me how I get an xml document with a DTD to display like a web page.

If css and xslt won't transform the xml document into a displayable form... what will?

I just don't get it...

johnl

1:09 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



hi,
this for instance is xml:


<?xml version='1.0' encoding='iso-8859-1'?>
<xml1>
<txt>read this</txt>
<urgent>do read this</urgent>
<txt>thank you</txt>
</xml1>

write it to the file myfirstxml.xml and then click on it.
without further ado a (modern) browser will apply an internal standard-transformation to it, converting the xml to html (using xslt) and then displaying the html.

by writing your own xslt-rules you can tell the browser to apply another (xml to whatever you like) transformation. if you eg. only want to show the txt-entries of the above example in green and the urgent ones in red you could write a file (let's call it myfirstxsl.xsl):


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>

<xsl:template match="/">
<html><head><title>myfirstxml.xml transformed by myfirstxsl.xsl</title></head>
<body>
<xsl:apply-templates/>
</body></html>
</xsl:template>

<xsl:template match="txt">
<div class="txt" style="background-color: green;">
<xsl:value-of select="."/>
</div>
</xsl:template>

<xsl:template match="urgent">
<div class="urgent" style="background-color: red;">
<xsl:text>!THIS REALLY IS URGENT! </xsl:text>
<xsl:value-of select="."/>
</div>
</xsl:template>

</xsl:stylesheet>

if you tell your browser to use myfirstxml.xml as input to its internal xslt-processor it will apply the transformationrules of myfirstxsl.xsl and generate the output you specified there.
you only will have to tell the browser to use other than its internal transformation-rules by adding a line to your file myfirstxml.xml, which then will read:


<?xml version='1.0' encoding='iso-8859-1'?>
<?xml-stylesheet type="text/xsl" href="myfirstxsl.xsl"?>
<xml1>
<txt>read this</txt>
<urgent>do read this</urgent>
<txt>thank you</txt>
</xml1>

so, if you have any valid xml-file you can transform it to nearly anything you want by telling how in an xsl-file and then using an xslt-processor. in this example we used the xslt-processor of a modern browser, but there also exist standalone xslt-processors.

if you get some xml-file you want to display as html, you will have to program your own xsl-rules to display it in the wanted output-format. your program also will heavily depend on the structure of the xml-data as well as on the wanted output format.

if you get many xml-files with the same structure then this might be worth the effort.

the sentence "our xml is also exported in such a way that it's very difficult (possibly impossible) to build an xslt for it." is nonsense (if they at least send you valid xml).

hope this helps..

ronin

1:29 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's fantastic, johnl, thanks very much for that insight as to how xslt and xml interact. I'll report back with how I get on, trying to untangle this document!