Cahners Business Information Wireless Internet
|    Home    |    Archives    |    ASP Directory    |    Event Calendar    |    Subscribe    
Wireless Week
Topics

Features
Above the Fray
Follow the Money
Market Research
Big Ideas
Technoptions
Developer Insights
Q & A
Opinions

Tools and Services

Archives
Calendar of Events
ASP Directory
Other Cahners Pubs

About Us

Submit a Release
Meet our Staff
Contact Us
Advertise
Magazine Subscription

Powered by...

Cahners

QuoteMedia

MyPoll.net



Printer-friendly format

Using XML/XSL For Wireless Application Development



In an earlier article, I discussed the development of a capable wireless application architecture using XML. In this article, I'd like to take this discussion a step further by introducing some real-world concepts through some simple sample code. Our goal will be to build a simple Web application that serves both HTML and WML content depending on the user's device type. If the user is using a Web browser, they receive HTML content; if they're using a WAP browser, they receive WML. For our simple application, we'll store an address book's contents in an XML file, keeping in mind that this XML content could also be dynamically retrieved from an enterprise relational database or some other data store.

To build this application, the following steps will need to be completed:

  1. Build the XML document.
  2. Build the XSL Stylesheets.
  3. Apply the stylesheet to the document.
  4. Add in code to detect browser type and respond accordingly.

1. Build The XML Document

For this example, we'll use a bit of ASP code (written using the VBScript scripting language) but the same techniques could be applied just easily using another server-side tool such as Java servlets, Perl, or PHP. The XML code for our AddressBook document appears in the following listing.

<?xml version='1.0'?> <?xml:stylesheet type='text/xsl' href='AddressBook_wap.xsl'?> <AddressBook> <Contact> <Name>Marc Robards</Name> <Email>mrobards@company.com</Email> <Phone>212.555.1212</Phone> </Contact> <Contact> <Name>Jeff Perkins</Name> <Email>jperkins@company.com</Email> <Phone>212.111.1234</Phone> </Contact> <Contact> <Name>David Blankenbeckler</Name> <Email>dblankenbeckler@company.com</Email> <Phone>212.999.9876</Phone> </Contact> </AddressBook>

The AddressBook XML Document

2. Build The XSL Stylesheets

As you can see, this XML document defines a root AddressBook node followed by children Contact elements to store contact information for individuals. Our next step will be to build two XSL stylesheets to format the XML into both HTML and WML (shown in the following two listings).

<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <wml> <card id="AddressBook" title="AddressBook"> <p class="copy"><b>Your Address Book</b></p> <xsl:for-each select="AddressBook/Contact"> <p class="copy"> <b>Name: <xsl:value-of select="Name"/></b><br/> Email: <xsl:value-of select="Email"/><br/> Phone: <xsl:value-of select="Phone"/><br/> </p> </xsl:for-each> </card> </wml> </xsl:template> </xsl:stylesheet>

The AddressBook_wap.xsl Stylesheet

<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <P class="headline">Your Address Book</h3> <xsl:for-each select="AddressBook/Contact"> <p class="copy"> <b>Name: <xsl:value-of select="Name"/></b><br/> Email: <xsl:value-of select="Email"/><br/> Phone: <xsl:value-of select="Phone"/><br/> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>

The AddressBook_web.xsl Stylesheet

3. Apply the stylesheets

In order to "apply" the XSL stylesheet to the XML data, we will need two pieces of software on our Web server that can be accessed from our ASP code. These two components, an XML parser and an XSLT processor, are available free of charge from Microsoft as one download known as the MSXML Parser. The following ASP code snippet shows how we would apply the AddressBook_web.xsl stylesheet to the AddressBook.xml document in order to produce HTML output.

<% Set xml = Server.CreateObject("MSXML2.DOMDocument") xml.async = false xml.load (Server.MapPath("AddressBook.xml")) Set xsl = Server.CreateObject("MSXML2.DOMDocument") xsl.async = false xsl.load(Server.MapPath("AddressBook_web.xsl")) Response.write (xml.transformNode(xsl)) %>

Transforming The XML Using The MSXML Parser

As you can see, this code loads up the AddressBook.xml document, then loads the AddressBook_web.xsl stylesheet. It's final action is to transform the XML document using the XSL stylesheet and print those results to the user. Our final step will be to detect the browser type and output the appropriate results back to the user.

4. Detect browser type and respond accordingly

In order to detect the browser type hitting our server, we can examine the HTTP_USER_AGENT variable using the Request.ServerVariables("HTTP_USER_AGENT") method call. To simplify things, I think we can safely assume that Web users will be using either Netscape Navigator (begins with "Mozilla") or Microsoft Internet Explorer ("MSIE") when accessing our site. (For a list of valid user agent values, visit this site). If it's not one of these two browsers, we'll assume - for this example - that the user is accessing our server via a WAP browser. The following code examines the user agent value and applies the appropriate stylesheet. Note that in the case of a WAP browser, we also need to output the appropriate MIME content type of "text/vnd.wap.wml".

<% Set xml = Server.CreateObject("MSXML2.DOMDocument") xml.async = false xml.load (Server.MapPath("AddressBook.xml")) Set xsl = Server.CreateObject("MSXML2.DOMDocument") xsl.async = false If (InStr(Request.ServerVariables("HTTP_USER_AGENT"), "Moz") OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "MSIE")) Then xsl.load(Server.MapPath("AddressBook_web.xsl")) Else xsl.load(Server.MapPath("AddressBook_wap.xsl")) Response.ContentType = "text/vnd.wap.wml" End If Response.write (xml.transformNode(xsl)) %>

Detecting and Handling Browser Types

While no means an exhaustive tutorial, this should provide you with a bit more "real-world" development information if you're considering an XML-based application architecture. For more information on XML/XSLT, an excellent starting point is XML.com. Happy coding!


Bryan Morgan is an independent writer and software developer. He is a columnist with Wireless Internet magazine and was the founder of WirelessDevNet.com in 1999.

 

Sponsors









|    Home    |    Archives    |    ASP Directory    |    Event Calendar    |    Subscribe    |

Copyright © 2001 Cahners Business Information
Use of this Web site is subject to its Terms of Use
Privacy Policy