Editor’s note: This article originally appeared in TechRepublic’s Web Development Zone TechMail. Subscribe, and you’ll receive information on Web-development related projects and trends.

By Les Hamilton

The problem with using Extensible Markup Language (XML) and Extensible Stylesheet Language (XSL) on the Web is that only Internet Explorer 5 and above can display it. While this is acceptable for intranets and public Web sites, where we can specify the use of IE5+ for our clients, what about those times when you want to make the same content available for Netscape or handheld users? A lot of developers have been forced to write and test their Web sites two or more times in order to achieve proper display formatting for their company’s sites.


Second in a series

This article from TechRepublic’s Web Development Zone describes issues related to integrating XML and Active Server Pages (ASP). Last week’s article discussed XML and XSL integration. Next week’s installment will look at XML, ActiveX Data Objects (ADO), and ASP.


Thanks to the foresight of Microsoft, it is possible to combine the XML and XSL on the server and send the resulting HTML code to a client browser. By using the Msxml3.dll in an ASP page, we can load and transform an XML document with an XSL style sheet and send the results to our users. Here is a shortened example (leaving out most of the error handling code) of the needed code for a Sample.asp.
<%@ LANGUAGE = JScript %>
<%
  // Set the source and stylesheet locations here
  var sourceFile = Server.MapPath(“sample.xml”);
  var styleFile = Server.MapPath(“sample.xsl”);
 
  // Load the XML
  var source = Server.CreateObject(“Microsoft.XMLDOM”);
  source.async = false;
  source.load(sourceFile);

  // Load the XSL
  var style = Server.CreateObject(“Microsoft.XMLDOM”);
  style.async = false;
  style.load(styleFile);
  result = source.transformNode(style);

  Response.Write(result);
%>

To run this ASP page, you need to have the Internet Explorer 5 Msxml.dll installed on your server. Create a test site on your machine and try viewing the ASP page and the XML files.

Below are the Sample.xsl file source and the Sample.xml that we want to present.

Sample.xsl:
<?xml version=”1.0″?>
<HTML xmlns:xsl=”http://www.w3.org/TR/WD-xsl”>
  <BODY STYLE=”font-family:Arial, helvetica, sans-serif; font-size:12pt;
        background-color:#EEEEEE”>
    <xsl:for-each select=”breakfast-menu/food”>
      <DIV STYLE=”background-color:teal; color:white; padding:4px”>
        <SPAN STYLE=”font-weight:bold; color:white”>
        <xsl:value-of select=”name”/></SPAN>
        – <xsl:value-of select=”price”/>
      </DIV>
      <DIV STYLE=”margin-left:20px; margin-bottom:1em; font-size:10pt”>
        <xsl:value-of select=”description”/>
        <SPAN STYLE=”font-style:italic”>
          (<xsl:value-of select=”calories”/> calories per serving)
        </SPAN>
      </DIV>
    </xsl:for-each>
  </BODY>
</HTML>

Sample.xml:
<?xml version=’1.0′?>
<?xml:stylesheet type=”text/xsl” href=”simple.xsl” ?>
<breakfast-menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>two of our famous Belgian Waffles with plenty
    of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>light Belgian waffles covered with strawberrys
    and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>light Belgian waffles covered with an assortment
    of fresh berries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>thick slices made from our homemade sourdough
    bread</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>two eggs, bacon or sausage, toast, and our
    ever-popular hash browns</description>
    <calories>950</calories>
  </food>
</breakfast-menu>

For more information, check out MSDN Online—XML Developer Center.