Transforming XML into WML

Photo of author

By Miro Stoichev

Introduction

In a previous article, we made our initial foray into XSLT and XPath, finishing with an example that demonstrated the transformation of XML into HTML. Now that we have seen the stylesheet for transforming XML into HTML, we can now look at how we can transform XML into WML.

Looking at the stylesheet below, you can see that it is quite similar to the stylesheet for transforming to HTML. Before we look into the differences, let’s see how this stylesheet can be applied to the XML document.

<?xml version=’1.0′?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″> <xsl:output omit-xml-declaration=”yes”/> <xsl:template match=”/”> <wml> <card id=”card1″ title=”Courses”> <p> <b>Course Title: </b><xsl:value-of select=”Course/Title”/><br/> <b>Synopsis: </b><i><xsl:value-of select=”Course/Synopsis”/></i><br/> <b>Duration: </b><xsl:value-of select=”(Course/Duration) div 8″/> days <br/> <b>Fees: </b>S$$<xsl:value-of select=”Course/Fees”/><br/> <b>Dates:</b><br/> <xsl:for-each select=”Course/CourseDates/Date[Day!=”]”> <xsl:value-of select=”Day”/>/<xsl:value-of select=”Month”/>/<xsl:value-of select=”Year”/> From <xsl:value-of select=”Time/From”/>hrs to <xsl:value-of select=”Time/To”/>hrs @ <xsl:value-of select=”Venue”/> – <xsl:value-of select=”(/Course/Duration) div (((Time/To) – (Time/From)) div 100)”/> <xsl:value-of select=”Mode”/> <br/> </xsl:for-each> </p> </card> </wml> </xsl:template> </xsl:stylesheet>

Recall that the earlier example performs the transformation on the client-side? That is, the web browser, IE5, performed the transformation. In the case of WAP, it is not possible for the WAP device to perform the transformation since they are typically “dumb” devices with limited processing power. What we need to do here is to perform the transformation on the server-side. In other words, the web server will transform the XML document into WML.

To do that, we will make use of the Microsoft Active Server Pages (ASP) technology and the Microsoft Document Object Model (DOM) to do the transformation:

<% ‘—Create an instance of the DOM object— Set xml = Server.CreateObject(“MSXML2.DOMDocument”) ‘—Let it run synchronously— xml.async = false ‘—Load the XML document— xml.load (Server.MapPath(“XMLCourse.xml”)) Set xsl = Server.CreateObject(“MSXML2.DOMDocument”) xsl.async = false ‘—Load the XSLT stylesheet— xsl.load (Server.MapPath(“WML.xsl”)) ‘—Set the MIME type— Response.ContentType = “text/vnd.wap.wml” ‘—Send the WML prologue— Response.Write <?xml version=’1.0’?> Response.Write “<!DOCTYPE wml PUBLIC “”-//WAPFORUM//DTD ” & _ “WML 1.1//EN”” “”http://www.wapforum.org/DTD/wml_1.1.xml””>” ‘—Performs the transformation— Response.write (xml.transformNode(xsl)) %>

You would need a web server and a WAP emulator to try this example. Here I am using the Microsoft IIS 4.0 and UP.Simulator by Phone.com:

Dissecting The ASP Document

We first look at the ASP document that performs the transformation. First, we create an instance of the DOM object using the MSXML3 and load up the XML document:

Set xml = Server.CreateObject(“MSXML2.DOMDocument”) xml.async = false xml.load (Server.MapPath(“XMLCourse.xml”))

Next we create another instance of the DOM object to load the XSL stylesheet, since an XSL stylesheet is also an XML document:

Set xsl = Server.CreateObject(“MSXML2.DOMDocument”) xsl.async = false xsl.load (Server.MapPath(“WML.xsl”))

We then prepare to send the MIME type for WML content and also the WML prologue:

Response.ContentType = “text/vnd.wap.wml” Response.Write “<?xml version=””1.0″” ?>” Response.Write “<!DOCTYPE wml PUBLIC “”-//WAPFORUM//DTD ” & _ “WML 1.1//EN”” “”http://www.wapforum.org/DTD/wml_1.1.xml””>”

Once the MIME type and the prologue have been sent, we use the transformNode() method to perform the transformation.

Response.write (xml.transformNode(xsl)) %>

That’s it! The XML document will then be transformed into WML and the WML content would be sent to the WAP emulator. I will talk about the MSXML3 in more details in subsequent articles.

Dissecting The XSL Stylesheet

The most notable addition to the stylesheet is the element:

<?xml version=”1.0″?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″> <xsl:output omit-xml-declaration=”yes”>

In our case, I use the element with the attribute omit-xml-declaration set to “yes”. This is important, as the XSLT processor (MSXML3, in this case) will include in the output the XML Processing Instruction:

<?xml version=”1.0″ encoding=”utf-8″?>

And so if element is omitted, the WAP device would receive the following (formatted for easy reading):

<?xml version=”1.0″?> <!DOCTYPE wml PUBLIC “-//WAPFORUM//DTD WML 1.1//EN” “http://www.wapforum.org/DTD/wml_1.1.xml”> <?xml version=”1.0″ encoding=”utf-8″?> <wml> <card id=”card1” title=”Courses”> <p> <b>Course Title: </b>XML/XSLT – Extensible Markup Language & Extensible Stylesheet Language<br/> <b>Synopsis: </b><i>XML is the language used for describing data. With the advent of WAP, web sites developers are increasingly deploying their sites in XML and using the transformation engine of XSL, which is XSLT, to tailor their web pages to different browsers. Participants will be developing applications that can dynamically adapt to different browsers. </i><br/> <b>Duration: </b>2 days <br/> <b>Fees: </b>S$$800<br/> <b>Dates:</b><br/> 14/10/2000 From 1730hrs to 2130hrs @ Rock Tower – 4Evenings<br/> 21/10/2000 From 0900hrs to 1700hrs @ Developers Unit – 2Full Days<br/> </p> </card> </wml>

The additional PI is going to cause an error since the resultant WML does not adhere to the WML DTD.

Conclusion

In these two articles, I have taken you on a roller coaster tour of what XSLT and XPath can do for you (in terms of XML transformation). I hope it is enough to get you excited about the technology and start thinking about how you can restructure your site using XML and XSLT.

XML and XSLT are gaining a lot of interest in the developer community because of its flexibility in tailoring content format. One of the key areas of is in B to B communication. Microsoft has realised this and this is clearly shown in the company’s .NET framework. All existing/future Microsoft products exhibit deep support for XML.

Leave a Comment