|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
XML Protocols Got XSLT?
Got XSLT?
By: Shouki Souri
Nov. 20, 2001 12:00 AM
XSLT is a powerful technology that can provide many benefits. In this tutorial we examine how you can use XSLT to transform an XML example to the WML (Wireless Markup Language) that
In Part 1 of this series (Vol. 2, issue 10) we briefly discussed how to transform our XML document into HTML by using Microsoft's Internet Explorer (MSXML parser 2.0). We were able to do this because IE 5.0 is more than just an XML parser - it's also an XSLT processor. This kind of transformation is sometimes called client-side transformation,
In the case of WAP, the WAP devices can't perform the transformation since they're typically "dump" devices with limited processing power and memory (at least for the time being). Therefore, in a real-world case, the transformation is most often performed on the server side. This tutorial requires the following software:
www.alphaworks.ibm.com/tech/LotusXSL http://developer.openwave.com/ LotusXSL implements an XSL processor in Java that can be used from the command line, in an applet or a servlet, or as a module in other programs. By default, it uses the XML4J XML parser (available as a xerces.jar file when you download LotusXSL) using xerces, but it can interface to any XML parser that conforms to the DOM level 2 or SAX level 1 specification. The freely available LotusXSL used to be a tool developed by alphaWorks, but has now been donated to the Apache project, which maintains it under the name Xalan. LotusXSL is a version (or distribution) of Xalan that has been tested and approved by IBM alphaWorks. Openwave's software developer kit, UP.SDK, is the leading solution for creating WAP 1.1-compatible Internet services and applications for wireless handsets. The freely available UP.SDK contains the tools and interfaces necessary for a developer to create innovative applications for the wireless Internet. UP.SDK includes the robust UP.Simulator software, available for Windows platforms and designed to accurately simulate the behavior of a variety of Internet-enabled handsets. Finally, you need a JVM since LotusXSL is a Java implementation of the XSL engine and needs a container to run.
* * *
The primary purpose of this tutorial is to introduce you to the following concepts:
WAP - Wireless Application Protocol - is the leading standard for information services on wireless terminals like digital mobile phones. The WAP standard is based on Internet standards (HTML, XML ,and TCP/IP). It consists of a WML language specification, a WMLScript specification, and a Wireless Telephony Application Interface (WTAI) specification. WAP is published by the WAP Forum, founded in 1997 by Ericsson, Motorola, Nokia, and Unwired Planet. Forum members now represent over 90% of the global handset market, as well as leading infrastructure providers, software developers, and other organizations.
What Is WML?
WML is used to create pages that can be displayed in a WAP browser. Pages in WML are called decks, which are constructed as a set of cards. WML is mostly about text. Tags that would slow down the communication with handheld devices are not a part of the WML standard, and the use of tables and images is strongly restricted. Since WML is an XML application, all tags are case sensitive (<wml> isn't the same as <WML>), and all tags must be properly closed. When a WML page is accessed from a mobile phone, all the cards in the page are downloaded from the WAP server. Navigation between the cards is done by the phone computer (inside the phone) without any extra access trips to the server. See the Resources section for more information about WAP and WML. Okay, enough theory stuff...let's gets started!
Recalling Our XML Document
Note that we won't reference any XSL stylesheet onto our XML document. This is because we'll explicitly plug in our WML, XSL, and XML documents to perform the translation from a command line. Our goal is to produce a WML page that looks like Figure 1, using UP.SDK simulator software.
Creating the Stylesheet Document
Understanding the WML Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl=Here we define the namespace for the XSL stylesheet with the <xsl:stylesheet> element. Note that we use the new W3C XSLT recommendation, which requires an additional attribute named version to be included. In the previous article we used MSXML 2.0, which is not 100% compatible with this recommendation, so we use the following line instead: <xsl:stylesheet xmlns:xsl=The <xsl:output> specifies options for use in serializing the result tree. In this case we output nicely indented XML: <xsl:output method=The <xsl:template> element: <xsl:template match="/">is used as a template to match against the source XML document. This is a template that corresponds to the root (/) of the XML source document. Now we begin to construct the body of our WML page using <wml> tags: <wml>The <xsl:apply-templates> element first selects a set of nodes using the query specified in the select attribute. If this attribute is left unspecified, all children of the current node are selected. For each of the selected nodes, <xsl:apply-templates> directs the XSLT processor to find an appropriate <xsl:template> to apply. In our case the XSLT processor will apply all ITEM templates (see Listing 3). Now we'll start to define those ITEM templates. The <xsl:element> element allows an element to be created with a computed name. The name of the element to be created is specified by a required name attribute (card) and an optional namespace attribute. In other words, this creates the <card> element with attributes id="Book<xsl:number/> and title="My Book Catalog". The element <xsl:number/> is an empty element and serves as a counter. Similarly, we created the element <do> with the attribute type="accept" and the label="Next Book". The last element we created is <go> with the attribute name href="#Book<xsl:number value="position() + 1". This translates to #Book2 (since the position of this template is 1). We close the card and do elements. The XSL <xsl:value-of> element can be used to select the value of the element matched by the select predicate. The XSL stylesheet then closes all open element tags in order, with no nesting allowed. Listing 4 is the second half of our stylesheet. The XSL <xsl:template match="ITEM[position() = last()]"> checks for the last ITEM element in our XML document. If it actually is the last element, the XSLT processor will continue with the next line; otherwise it will skip and apply <xsl:template match="ITEM"> again. The rest is pretty simple, except for element go attribute <xsl:attribute name="href">#Book<xsl:numbervalue="count(//ITEM) - count(//ITEM) + 1"/></xsl:attribute>. The XSLT count() function returns the number of nodes in the argument node-set. In our example it returns three (since there are only three books in our XML document). Thus the result is an attribute value of #Book1, which forces the WML browser (or our WAP simulator) to go back to the Book1 card. I'm sure there are several other and possibly easier ways to create this behavior using other XSLT functions, but I'll leave this as an exercise for the reader.
Performing Your Own Transformations
Java.org.apache.xalan.xslt.Process provides a basic utility for performing transformations from the command line. The command line for most standard transformations is as follows: java org.apache.xalan.xslt.Process -where xmlSource is the XML source file name, stylesheet is the XSL stylesheet file name, and outputfile is the output file name. If you want the output to be displayed onscreen, simply omit the -out flag and argument. Both mybooks_.xml and mybooks_wml.xsl are saved under gotxslt directory as d:\gotxslt\*.* To perform the translation, type the following: D:\>cd gotxsltIf all goes well, the new output file mybooks.wml will be the resultant output from the XML to WML translation (see Figure 2). The file mybooks.wml is a pure WML ASCII file, so you can open it and study it in any text editor. Testing the Deck In order to test our WML output file, you need to start the UP.Simulator program (upsim.exe) and type the URL of your file (in this case file://d:/gotxsl/mybooks.wml) in the Go field and press Enter. The results of a successful request are shown in Figure 3: Upon clicking the Next button (which is just below the text labeled "Next Book"), a second card will display the second book. Clicking again for our third book, you'll see that this time the label shows "Back" (see right-hand picture in Figure 3). Clicking on the Next button again will take you to our first book, and the loop will continue.
Conclusion
In Part 3 we'll translate our XML file into VoiceXML. Resources
Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
||||||||||||||||||||||||||||||||||||||||||||||||||||