XML and XSL

They have: 4 posts

Joined: Apr 2000

I read some things about XML and how great it is, but I don't quite understand how it works. It's client side (since browsers need to read it) but how does incorperate database access, etc.? Wouldn't that need to be server side?

Also, what is XSL? I saw it was the new templating system for OpenTopic but I'm not sure how it works or what it exactly is?

Basically, I need a human explanation of these two markup languages rather then the big, long, complicated definations that I have been reading and unable to understand.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

AnonDude,

I'm not an XML expert but I have begun to use it so I'll try to explain it. We'll focus on your database question.

Basically, using a server-side language (like ASP) I can query a database and store that record in an XML file using the file scripting object. Because XML allows you to use custom tags, I can name them the same as the names in my database, so let's say I queried a phone number database, I could store the results in an XML file like this:

<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="phone_database.xsl"?>
<PHONEDATABASE>
<PHONENUMBER>
<NAME>Joe Smith</NAME>
<NUMBER>123-4567</NUMBER>
<NOTE>This guy is a lamer</NOTE>
</PHONENUMBER>
<PHONENUMBER>
<NAME>John Smith</NAME>
<NUMBER>890-1234</NUMBER>
<NOTE>This guy is not a lamer</NOTE>
</PHONENUMBER>

And so on... Now I'll try to answer your second question. See that reference to phone_database.xsl above? That's our XSL stylesheet that will transform our plain data XML to something more presentable.

Here's the source for our XSL stylesheet:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1" bgcolor="blue">
<tr>
<th>Name</th>
<th>Number</th>
<th>Notes</th>
</tr>
<xsl:for-each select="PHONEDATABASE/PHONENUMBER">
<tr>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="NUMBER"/></td>
<td><xsl:value-of select="NOTE"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Basically, XSL file is a template for your data.

The possibilities for xml are pretty exciting. One thing you have to watch is your coding, XML follows a strict nesting rule (case sensitive also). If you're good at nested tables then you shouldn't have a hard time.

I hope that helps.

Regards,
Peter J. Boettcher

PJ | Are we there yet?
pjboettcher.com

They have: 4 posts

Joined: Apr 2000

Ahhh! I get it. Why can't the tutorials be that clear?

So basically what happens is the I write a program which gets some information and stores that into an XML file into its tree structure. The end user downloads the XML file (which then includes the XSL template). The browser then parses them both and shows the data accordingly...

To put it simply, the XML file defines the data and the XLS file defines how it should be displayed, correct?

Also, I have seen a few libraries/modules in PHP and Perl about parsing XML code. Wouldn't you want to generate it more then parse it? Why would you parse it? I thought the idea was to generate the XML document, send it to the browser, and have that parse it according to the XSL template. Am I missing something?

[This message has been edited by AnonDude (edited 15 April 2000).]

They have: 4 posts

Joined: Apr 2000

Ok. Let me try and outline to see if it got through to me.

XML is good because it saves the database a few queries. The data is already in a downloadable form (for IE5 browsers right now) or can be parsed by the server into HTML otherwise. Its usable anywhere because its the actual data being passed to the client in a manner which can be manipulated many different ways both server and client side.

I thought XML would be useful to me right now, but I'll have to pass -- for now. The reason is I primarily use PHP and Perl, both which require a seperate C parser to parse XML. If I distributed a program using this technology, it wouldn't get much publicity due to its requirements. But I think if I ever develop a program for a single company which allows me to install the parser, I will definately use it.

But at least now I think I know where I stand with the XML technology and understand why it would be useful to me. Thanks a bunch for answering all my questions.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

AnonDude,

Parsing is done on the server when you're dealing with a straight XML data file (no import from a database, although you could but this would be counter-productive) and you want to be able to send the data to a browser that can't handle client-side parsing (which is still most of them).

This is done by creating an instance of the XML object, and then pulling your XML data and XSL stylesheet info into an ASP (or PHP) file.

Regards,
Peter J. Boettcher

[This message has been edited by Peter J. Boettcher (edited 16 April 2000).]

PJ | Are we there yet?
pjboettcher.com

They have: 4 posts

Joined: Apr 2000

Then what's the advantage of XML? Isn't it just easier (and faster) to keep the data in the database rather then in an XML file and have your Perl/ASP/PHP script use a template mechanism?

Obviously parsing an XML file will be slower anyday then sending a query to a database and echoing the results... so why store anything in XML? (I know I'm missing something quite big here - its not quite clicking yet).

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

AnonDude,

The server-side XML is mostly used so when you're developing XML pages, you don't have to worry about older browsers.

So all other users besides IE5 will get the server version while IE5 users will get the client-parsed version.

I know it's a little bit strange right now, but as soon as more browsers support it, it will be a big deal. Think about a db server that gets millions of hits, lets say a B2B financial application. XML has the ability to take a tremendous load off the db server, First by passing much of the load to the client by client-side parsing. And second, by storing common queries in external XML files.

But perhaps the coolest thing is the data. You have to remember the data inside an XML document is data, not just source code. So think about all the programs that are going to take advantage of that, palm uploads, Intranet data apps, basically anything you run on your computer, and it's not platform dependant.

When you use ASP or PHP to query a database all you get back is source code written to the screen. Sure, you can store it in some scripting arrays but it's still just source code, with XML it's true data that can be used anywhere.

There's still a lot of issues but that's no different then any new language.

Hope that helps (did I mention that XML data can be used anywhere?, ok just making sure)

Regards,
Peter J. Boettcher

PJ | Are we there yet?
pjboettcher.com

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.