|
|
||||||
|
|
![]() |
|
![]() |
|
||
|
|
||||||
Serving XML with Active Server Pages
As it turns out, there are a lot of ways to serve XML. If you're a Perl programmer, you can use the
XML::Parsermodule in Perl to load XML documents into the DOM. TheXML::Parsermodule is definitively top-shelf software. The module was developed by Larry Wall, Perl's inventor, and later modified by Clark Cooper. The module provides an interface to James Clark's Expat parser, which coincidentally will be the parser of choice in the long-awaited Navigator 5. (I also have it on good authority that Infoseek uses the Expat parser in its add-on to Ultraseek Server.)As I've previously described, you can also serve up XML using Java servlets. One thing I haven't mentioned is the Cocoon project, which is an effort by developers from the Apache Group to add XML capability to the Apache Web server. Cocoon itself is a Java servlet that specifically supports XML and XSL. Cocoon by default uses the OpenXML parser and XSL:P to process style sheets. However, Cocoon is also designed to work with IBM's XML for Java (XML4J) and LotusXSL processors, and the processors that are included with Sun's ProjectX.
Did I say ProjectX? Right, that's Sun's effort to enable Java applications with the power of XML. Java ProjectX includes both validating and nonvalidating parsers that conform to the XML 1.0 Recommendation. The distribution also supports the Document Object Model (DOM) Level 1 specification, and Simple API for XML (SAX) 1.0. Throw in a servlet and a few Java Server Pages and you have a solution for delivering XML dynamically.
This month, I would like to look at another solution for delivering XML dynamically. That is, the use of Active Server Pages (ASP). By combining ASP with the MSXML parser, you automatically have a quick and reliable solution for XML. Of course, to deploy the examples presented here, you must have Microsoft Internet Information Server (IIS) running on Windows NT Server Edition. You can also experiment with these samples using Personal Web Server (PWS) on Windows 98. On Windows NT Workstation, you can use PWS or the older Peer Web Services. I should mention that you'll need some familiarity with ASP to use the techniques described here. (See "Programming for Active Server Pages," Web Techniques, October 1997.)
ASP Review
As you likely know, ASP is built in to IIS and lets you embed JScript or VBScript into HTML pages residing on the server. Using this scripting code, ASP lets you generate Web content dynamically. Now, you can combine ASP with the MSXML parser to instantiate a DOM object, load XML documents, and manipulate them using JScript or VBScript in much the same way you can in Internet Explorer.
Scripting with ASP is a simple process. All server scripts are contained within the
<%and%>delimiters. In addition, ASP supplies two directives. The first,<%@, is a processing directive that lets you specify, among other things, the scripting language you plan to use. The scripting language can be specified for just the current page, or for all related server pages. The second directive,<%=, is called the output directive and is used to send text-based output back to the browser.You have access to six server objects you can use to gather browser information during a request, to send responses, to get server variables, to manage cookies, and so on. These six built-in objects are
Application,Request,Response,Server,Session, andObjectContext.The two objects we're interested in for this article are
RequestandResponse.Requestlets you get information sent with an HTTPGETorPOSTrequest. TheRequestobject has just one method,BinaryRead, and one property,TotalBytes. When a request comes in, data is stored in a collection, so theRequestobject returns one of the following five collections:ClientCertificate,Cookies,Form,QueryString, andServerVariables.The other object,
Response, provides similar collections along with some methods you can use to send data back to the browser. The supported methods areAddHeader,AppendToLog,BinaryWrite,Clear,End,Flush,Redirect, andWrite. TheResponseobject also supports several properties includingBuffer,CacheControl,Charset,ContentType,Expires,ExpiresAbsolute,IsClientConnected,PICS, andstatus. The only collection supported byResponseisCookies.Methods for Processing
As you might imagine, there are several methods to access and process XML data on the server. For example, a publishing site could take a file-based approach, storing individual XML documents alongside HTML files. In this case, you'd simply apply a style sheet to requested documents and serve the result document back to the browser.
There are times when you might wish to construct XML markup from raw data, such as data from a database. Within an ASP script, you could construct a well-formed document as you pull individual fields from the database. Alternatively, you can employ Microsoft's ActiveX Data Object (ADO) to import raw data into an XML DOM object. The ADO uses database field names to automatically assign element names. The field's data makes up the content. Of course, the data source must be an ADO-compliant database such as Access, and you must be using ADO 2.1 or later.
Preparing Your Server
The first thing that I should mention is that your server must support ASP. This is a given in IIS, but ASP is not automatically supported in PWS or the Peer Web Services. The only way to gain such support is to go to the Microsoft Windows NT Server Web site and grab the latest version of the NT Server Options Pack. When you install the Options Pack, you'll have an opportunity to add the ASP services.
With ASP support installed, you must configure the server for XML. This includes adding the XML processors, and registering the XML file and MIME types with the server. One very simple way to do this is to install Internet Explorer 5. When you install IE, it installs MSXML.DLL (which contains the XML and XSL components), and registers both the text/xml MIME type and the XML file type with the operating system. At this point, the DOM API is available to you through scripting (JavaScript and VBScript) and programming (C++ and Java), and you can send and receive XML from the server over an HTTP connection.
Once you've installed the proper software, you should test the installation to ensure that the server recognizes your ASP pages. First check that the server process is running (see your documentation for details). If you're connected to a network, this step is easy. Simply point your browser to the home page. This should bring up the default.asp page, which performs a redirect to the welcome.htm file in the samples directory.
Combining XML with ASP
One of the benefits of using ASP is that you can generate XML on the fly. For example, you can take an existing document and choose a style sheet based on user preferences, or on the type of browser making the request.
In fact, the combination of ASP and a style-sheet engine gives you several alternatives for implementing the same task. Consider a book database or catalog of books where the user wants to locate all of the XML books on file that discuss processing XML in Java. Depending on how the database is implemented, the query involves an indexed search for two words. One word carries more weight than the other. That is, you want to locate XML books that include Java techniques. You don't want a book that primarily covers Java with a few XML tidbits thrown in. In an attempt to get all possible results, some applications often relax exact word matches, so they might return, say, all of the JavaScript books in your result set. All right, so what happens?
The task begins with the user filling out a search form. When the user hits a submit button, the form gets zapped to the server. In ASP you can easily get the form's data using the
Requestobject described earlier. To get the query using JScript, you might use something like:<% if Request.Form("xmlForm") == "True"
{
searchStr = Request.Form("formQuery");
}
%>
OK, so you grab the user's query from the form's data. Now you want to process the query. As it turns out you have a couple of choices. You can make the typical database query and attempt to filter the results in a script, or you can load the results into a DOM object and perform the filtering using XSL.
Serving XML Pages
The most common task you'll likely carry out is serving an XML document. That is, you have a page that's marked up in XML. Your goal is to take that XML document, apply a style sheet to it, and return the result. Here's how you do it. First create the Active Server Page in Listing One.
The first thing this script does is set the language to JScript. By default, ASP uses the VBScript scripting engine. After adding some opening HTML tags, this example includes a script, as designated by the
<%and%>delimiters. The steps in the script are to create a new DOM object to represent the XML document. I use theServerobject'sCreateObjectmethod to perform this task. You can also create this object outside of a script using the following within the HTML:<OBJECT RUNAT="server"
PROGID="Microsoft.XMLDOM"
id="xmlDocument">
</OBJECT>
You also need to create a second DOM object to represent the style sheet. (Remember, the style sheet is also an XML document.) Once you create these two objects, you need to load the XML documents into them. Assuming these are static documents residing on the server, you want to apply the server's
MapPath()method to resolve the relative path. The result of this operation is passed to the document'sload()method.The next step is to apply the style sheet to the XML document. Microsoft adds a DOM extension, called
transformNode(), that's designed for this purpose. The way it works is that you take the XML document instance and call itstransformNode()method, passing the style-sheet object as an argument, like so:xmlDocument.transformNode
(xslDocument)
The method takes
xslDocumentand applies it toxmlDocument. The result can be passed directly back to the browser using the server'sResponse.Writemethod.Creating an XML Validation Service
The next example shows how you can set up an XML validation service on your Web site that lets visitors send an XML document to your server. The service simply loads the XML document into the parser and informs the user if the document is well-formed and whether it is valid. The server page that performs this task is shown in Listing Two.
Once again, the server script creates a DOM object to represent the XML document. The example assumes that the form being posted contains the XML document string. That string is retrieved from the form and assigned to the
xmlDocStrvariable.By default, the MSXML parser checks only to see if a document is well-formed XML. However, the MSXML parser has a nifty feature that lets you toggle validation on or off. Turning this switch on causes the parser to validate the document and report errors if any occur. To turn validation on, simply set the
validateOnParseproperty totrue. Then, you can check for parse errors by examining theparseError.errorCodeproperty. If an error occurs, you can get a text summary of the error by reading theparseError.reasonproperty. You can direct the output to the client using the server'sResponse.Write()method.Conclusion
Going through this process, I have found that ASP combined with XML makes a very powerful combination. By using ASP, you gain the ability to generate XML dynamically. ASP also lets you maintain state in between sessions and get seamless access to ODBC databases. More importantly, the two technologies integrate well, making them easier to use than I had expected. Finally, using the MSXML parser lets you process and display XML documents using scripting code similar to what you'd use in Internet Explorer. Given how accessible XML has become, I hope to see more XML-based Web sites in the future.
(Get the source code for this article here.)
Michael is the author of Building Web Sites with XML from Prentice Hall. He provides XML training to large companies and publishes BeyondHTML.com. Michael also serves as Web Techniques' editor at large. He can be reached at mfloyd@lifestylesSantaCruz.com.
|
|