
Using XML/XSL For Wireless Application Development
by bryan morgan
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:
- Build the XML document.
- Build the XSL Stylesheets.
- Apply the stylesheet to the document.
- 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.
Marc Robards
mrobards@company.com
212.555.1212
Jeff Perkins
jperkins@company.com
212.111.1234
David Blankenbeckler
dblankenbeckler@company.com
212.999.9876
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).
Your Address Book
Name:
Email:
Phone:
The AddressBook_wap.xsl Stylesheet
Your Address Book
Name:
Email:
Phone:
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.
|