To read the article online, visit http://www.4GuysFromRolla.com/webtech/081300-1.shtml

Online Content Creation using ASP, XML and XSL

By Arno Jansen


This article will describe a simple example on how XML can be used in combination with ASP and XSL to let surfers create content online. I assume you already know what XML and XSL are and how parsing of an XML is handled (in tree structure). If you don't, I have written a detailed explanation of the XML document and how the XSL stylesheets are used to translate the XML data to HTML: Using XSL Stylesheets to Translate XML into HTML. Also I would highly recommend that you read the articles in the XML Article Section on 4Guys.

Imagine we are assigned a job to enable visitors of a website to create their own content. The "rules of engagement" are to use ASP for the dynamic part and XML to model the data. Of To keep the project as a whole simple to overview, I designed a small application that uses XSL stylesheets to translate the XML data to HTML. I then created two stylesheets, one for presenting the XML data into a viewable HTML file and one to translate it to an HTML form to let surfers edit the contents.

For this article we will use an XML file that contains a memo structure which contains a FROM field, a TO field, a SUBJECT line and the MESSAGE. The XML file for this structure looks like this:

<?xml version="1.0"?>
<memo>
  <from>Arno Jansen</from> 
  <to>XML developers</to>
  <subject>Little XML example</subject>
  <message>This is a small XML example using HTML forms</message>
</memo>

As I already described, we have two stylesheets: one for viewing and one for editing the memo. Because we want to use both stylesheets, we cannot specify a stylesheet in the XML file itself and send that file to the surfer's browser. Also, currently most browsers do not support XML (fully), so the ideal situation would be to process the XML file and one of the two stylesheets and just send the HTML output to the client. To accomplish that, we are going to use an ASP file that does the processing for us. We can link to the ASP file and supply one parameter on the querystring to let it know in what mode we want the memo to be (viewmode or edit mode), so a link would look something like this:

<A HREF="processmemo.asp?mode=view>
   Memo in View mode
</A>

for viewing and like this for editing:

<A HREF="processmemo.asp?mode=edit>
   Memo in Edit mode
</A>

Now, let's have a look at the ASP file (processmemo.asp) that is responsible for displaying either the XML data in either a viewable or editable mode. Before we start translating the XML into the appropriate HTML, we need to know in what mode we have to present the memo, so we use Request.QueryString to get the value of the mode. To get the right stylesheet for that mode, we add .xsl to it, and we have the filename of the stylesheet. So, in our case we named the two stylesheets: view.xsl and edit.xsl.

  stylesheet = request.querystring("mode")
  stylesheet = stylesheet & ".xsl"

Next step is to create two instances of the XML parser object, one for the memo-XML file and one for the XSL stylesheet (because XSL is in fact just another XML file).

  set xmlDocument = Server.CreateObject("Microsoft.XMLDOM")
  set xslDocument = Server.CreateObject("Microsoft.XMLDOM")

Then we parse both the memo XML file and the stylesheet file in their instances:

  xmlDocument.load(Server.Mappath(memofile))
  xslDocument.load(Server.Mappath(stylesheet))

And finally apply the stylesheet to the memo file and send the result to the surfer's browser. This is done using the transformNode method which we give the stylesheet we just parsed.

  Response.Write(xmlDocument.transformNode(xslDocument))

Doing this, just HTML is sent to the browser of the surfer. This way, you can serve XML files to a browser that is not able to handle XML. This does, however, require a server that can handle XML. I used IIS in combination with an installation of IE 5 on the server. IE5 comes with the XML parser COM object. If you do not want to install IE5, but you do want the XML parser object, Microsoft provides a redistributable package of the package on their MSDN website on XML (http://msdn.microsoft.com/xml/default.asp).

Designing for Different Browsers
If you use server-side XML processing in combination with a browser detection mechanism, you can use different stylesheets to create HTML that utilizes the different browser capabilities.

In Part 2 we'll examine the ASP page this is responsible for both presenting the viewable and editable formats of the XML data!

  • Read Part 2


  • Article Information
    Article Title: Online Content Creation using ASP, XML and XSL
    Article Author: Arno Jansen
    Published Date: Sunday, August 13, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/081300-1.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers