SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Building Smart Pages with ASP, XML and XSL : The ASP Script

Our ASP page checks the browser you are using and delivers the appropriate. If you are not running Internet Explorer 5.0 we open the XML document, apply the XSL transformation, and send the resulting HTML to the client. In Internet Explorer 5.0 we send the XML and XSL files directly to the client for processing.

Creating this logic is very easy. The first step is to make sure you have the Microsoft XML and XSL parsers installed on your server. If you are running Internet Explorer 5.0 on your server these parsers are already installed. If not, you need to download and install them from Microsoft's XML site.

Next you need to author the server-side script. The server script first determines the client's browser. This can either be accomplished using the IIS Browser Capabilities object or by analyzing the HTTP header directly. We do our browser detection by analyzing the header directly:

Dim sAgent,b_IE,b_Vers,b_Mac,b_Nav,b_Other
Dim ie4win,b_win,ie4,ie3,ie4mac,ns4

b_IE=false
b_Nav=false
b_Other = false
b_Vers = 0
sAgent = Request.ServerVariables("HTTP_USER_AGENT")

if (inStr(sAgent,"MSIE")>0) then
  b_IE=true
  b_Vers = Mid(sAgent,inStr(sAgent,"MSIE")+5,1)
elseif (inStr(sAgent,"MSPIE")>0) then
  b_Vers = Mid(sAgent,inStr(sAgent,"MSPIE")+9,1)
else
  b_Vers = Mid(sAgent,9,1)
end if

if (not (b_IE)) then
  b_Nav=inStr(sAgent,"Mozilla")>0 or inStr(sAgent,"compatible")>0
end if

b_Win = (instr(sAgent,"Win")) > 0 'Windows
b_Mac = (instr(sAgent,"Mac")) > 0 'Mac
b_Other = not (b_Win or b_Mac)    'Other

ie4 = b_IE and b_Vers>=4   ' IE4
ie5 = b_IE and b_Vers>=5   ' IE5
ie3 = b_IE and b_Vers<4    ' Everything <IE4 we consider IE3
ie4Mac = b_Mac and ie4     ' IE4 Mac
ns4 = b_Vers>=4 and b_Nav  ' NS4
ie4Win = b_Win and ie4 and not b_mac ' IE4 Windows

The next step is to write your script that uses the browser version to return the correct document. If you are running IE5, we send the XML document directly to the client, otherwise we generate the HTML on the server:

if ie5 then
  ' Send the XML File 
  response.write(OutputDocument("simple.xml"))
elseif (ie4) then
  ' Transform with simple.xsl to HTML + CSS
  response.write(TransformDocument("simple.xml","simple.xsl"))
else
  ' Transform with simpleHTML.xsl to HTML 3.2
  response.write(TransformDocument("simple.xml","simpleHTML.xsl"))
end if

At this point, we recommend you view our simple demo page. This is the same XML demonstration page from our first article. However, now it is viewable by all browsers. You should view the demo page and source in both Internet Explorer 4.0 or later and Netscape Navigator. You will notice the HTML is different depending upon your browser, and in the case of IE5, you are viewing the XML file directly. While the file appears different on the client, the presentations were created from the same source XML document (all the files are available for download at the end of this article). Since the presentation is separate from the source article, we only need to update one file to change the original article. We can also tweak the layout of all articles just by changing the appropriate XSL style sheet.

The next step is to explain how we perform the transformations on the server by explaining TransformDocument and OutputDocument functions.