Creating the WML Deck
Every WAP application uses a required set of WML elements. Pay particular attention to the concepts within the rest of this chapter. The following elements are used to some degree with every WAP application you create.
Note
Besides simply reading the following examples, type these code examples on the text editor of your choice. Then, try running them from a device-emulator. Understanding and writing the WML elements will play an important part in learning WAP development.
Save your WML files in an easily retrievable directory, such as C:\My Documents.
The WML Element
Every WML deck must contain three things: the document prologue, an element that designates the WML deck, and at least one card. You've already looked at the document prologue previously in this chapter. Now let's focus on the elements that designate the WML deck, specifically the WML elements.
The WML element is a simple, but powerful element. The WML element consists of no attributes, always follows directly after the document prologue, and is always the last tag element of a WML file. The following is a quick example of the WML element in action. Pay particular attention to the areas of code in boldface.
This example creates a WML deck with one card. Save this file as deck.wml. Figure 3.3 shows this code on a device screen:
1 <?xml version="1.0"?> 2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 3 "http://www.wapforum.org/DTD/wml_1.1.xml"> 4 <wml> 5 <card id="card1"> 6 <p>This is the only card within this WML deck.</p> 7 </card> 8 </wml>Figure 3.3 The deck.wml file on a device screen.
Note
When you click the Accept button (this button has the Back label above it), you are returned to the default main menu of the device-emulator. This main menu displays as your default page until you add navigational functionality to your applications (you'll do this in the Chapter 5).
The following is an explanation of this example:
The first line specifies the XML version number.
The second line specifies the public document identifier.
The third line specifies the location of the WML document type definition.
The fourth line uses the WML element to begin the WML deck. All WML decks must begin with a <wml> tag and end with a </wml> tag.
The fifth line uses the <card> tag to start a card.
The sixth line uses the P element to start a paragraph.
The seventh line uses the </card> tag to end the card.
The last line uses the </wml> tag to end the WML deck.
The CARD Element
All WML decks require at least one card. The CARD element is one of the most widely used WML elements in WAP development. Think of cards as the screens of an application, and you can see the importance of the CARD element.
Cards have a lot of functionality, much more than the WML element. Table 3.3 lists the attributes available for the CARD element. Although there are a lot of them, the main attributes to concern yourself with are id and title.
Table 3.3 The Attributes of the CARD Element
Attribute |
Required |
Description |
id |
No |
Specifies a name for the card. For easier programming, avoid using an id longer than eight characters. |
newcontext |
No |
A true or false (Boolean) value that specifies whether the device should initialize the context whenever the card is loaded. The default value is false. |
onenterbackward |
No |
Specifies the URL to open if the user navigates to this card through a PREV task (discussed in Chapter 5). |
onenterforward |
No |
Specifies the URL to open if the user navigates to this card through a GO task (discussed in Chapter 5). |
ontimer |
No |
Specifies the URL to open if a specified TIMER element expires. |
ordered |
No |
A true or false (Boolean) value that specifies the organization of the card content. The default is true. |
title |
No |
Specifies a brief text label for the card. |
The id attribute provides the card with an identifier. Identifiers are something of major importance, especially in navigating from one card to another. The id attribute also allows cards to access other cards in other, external decks.
The title attribute is the name of the card presented to the user. This attribute can also provide more advanced functionality such as creating pop-ups or bookmarks. Not all WAP devices use a browser that present title attributes.
Note
Don't worry if a WML element you're using isn't supported by all WML browsers. If a device does not support a WML element, the device browser ignores the element.
Most device manufacturers supply documentation regarding the WML elements their products do and do not support. For the most part, most WAP devices support all elements discussed in this book.
The syntax for using attributes is to start the element, and then set the attributes equal to a value. All this information is then set within brackets.
For example, in the case of the CARD element that uses the id and title attributes, the syntax would be something similar to this:
<card id="card1" title="Card 1">
When incorporating more than one card into a WML deck, you must include a unique id attribute for each card. The following example displays a WML deck with multiple cards and creates an application that allows users to navigate between the two cards within a deck. Save this file as card.wml. Figure 3.4 displays the card.wml file on a device screen.
Figure 3.4 The card.wml file on a device screen.1 <?xml version="1.0"?> 2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 3 "http://www.wapforum.org/DTD/wml_1.1.xml"> 4 <wml> 5 <!-- This is card 1 --> 6 <card id="card1" title="Card 1"> 7 <do type="accept" label="Next"> 8 <go href="#card2"/> 9 </do> 10 <p>This is card 1</p> 11 </card> 12 <!-- This is card 2 --> 13 <card id="card2" title="Card 2"> 14 <do type="accept" label="Back"> 15 <go href="#card1"/> 16 </do> 17 <p>This is card 2</p> 18 </card> 19 </wml>
Note
This example uses the DO element, which is discussed in more detail in Chapter 5 when you look at creating navigation within your applications.
The overall functionality created in this application is that by clicking the Accept button, the application navigates to one of the two cards in this deck.
The following is a line-by-line explanation of this example:
The sixth line uses the <card> tag to start a card. In addition, the id and title attributes are used.
The id attribute sets the reference name of the card to card1. This name is useful as your applications become more advanced and you begin to incorporate navigational functionality.
The title of the card is Card 1. If the WAP device you're using supports the title attribute of the CARD element, the text Card 1 displays when accessed. Otherwise, if the device does not support this attribute, the title does not display. This isn't a big deal, but you should be aware of such factors.
The seventh line uses the DO element to assign functionality to a user interface on the device. This line of code specifies that the device's Accept button be used for some sort of functionality. The label that displays above this Accept button is Next.
The eighth line uses the GO element (which is one of WML's task elements). This line of code assigns a link to the user interface defined in the previous line of code. In this example, whenever the Accept button is clicked, users are taken to the card2 card.
The ninth line uses the </do> tag to end the DO element.
The tenth line uses the <p> tag to start a paragraph.
The eleventh line uses the </card> tag to end the card.
Lines 1318 contain functionality similar to that found in lines 611. The only difference is that these lines create a second card named card2. This card also uses a DO element. In this second card, the DO element specifies the Accept button to return users to card1 when clicked.
The HEAD Element
Before concluding this chapter, we should discuss the HEAD element. The HEAD element provides the capability to specify information about the WML deck as a whole. Although you won't use this element within the applications in this book, the HEAD element is useful, especially for creating security within your WAP applications.
The HEAD element does not contain any attributes. Rather, it must contain one of these two elements:
ACCESS
META
The following sections examine these two elements and the functionality they provide within a deck-level header.
The ACCESS Element
The ACCESS element provides the functionality that restricts unauthorized users from accessing content-sensitive information. You won't use the ACCESS element in the examples in this book, though.
The ACCESS element contains two attributes, summarized in Table 3.4.
Table 3.4 The ACCESS Element Attributes
Attribute |
Required |
Description |
domain |
Yes |
The URL domain of other WML decks that can access the cards within the current deck. The default value is the domain of the current deck. |
path |
Yes |
The URL root of other WML decks that can access the cards in the current WML deck. |
The META Element
The META element provides information to the browser about meta- information. Meta-information tells a device to treat the data in a specific way.
Not all WAP devices support every meta-information type. The device browser ignores any meta-information it does not support. Refer to the device manufacturer's documentation if you have questions about whether a particular type of meta-information is supported by a WAP device. Table 3.5 lists the attributes included with the META element.
Table 3.5 The META Element Attributes
Attribute |
Required |
Description |
content |
Yes |
Specifies the metadata value to assign to the property. |
forua |
Yes |
A true or false value that specifies whether the content is intended for the device's browser (rather than, say, a proxy server or some other program). |
name |
Yes* |
The property name that represents the meta-information. |
property |
Yes* |
Used in place of the name attribute to specify information the browser should interpret as an HTTP header. |
schema |
No |
Form or structure used to interpret the property value. |
* Define either the name or the property attribute when using the META element.
In the world of WAP development, a common example of meta-information is the cache duration of a WAP device. Cache refers to previously downloaded material.
Caching is a WAP device's attempt at remembering the history of the data accessed. You can set a time limit within the META element that specifies how long the device will save cached cards.
Let's look at a quick example of how you could use the META element to change the memory cache.
The default cache for most devices is 30 days, or until the memory has exhausted. A developer does not need to define the cache, unless you want to lengthen, shorten, or disable the cache.
Note
For the most part, you should use a device's cache. Doing so improves the performance of a WAP application. Cache memory also enables cards to reload quickly because they're already stored within the device. Cards load more quickly from the cache than they do if accessed from a remote server.
However, some WAP developers like to disable the cache when working with cards that use user-inputted information. Disabling the cache ensures that the correct information is always used with a card.
The following example disables the memory cache. Save this file as head.wml. Figure 3.5 shows this example on a device screen:
1 <?xml version="1.0"?> 2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 3 "http://www.wapforum.org/DTD/wml_1.1.xml"> 4 <wml> 5 <head> 6 <meta http-equiv="Cache-Control" content="max-age=0" forua="true"/> 7 </head> 8 <card> 9 <p>The memory cache has now been disabled.</p> 10 </card> 11 </wml>
Figure 3.5 The head.wml file on a device screen.
The following is a line-by-line explanation of this example:
The fifth line is the start tag of the HEAD element.
The sixth and seventh lines instruct the device to drop the deck from the cache immediately by setting the max-age to 0. To length or shorten the time interval of the cache, the time interval must be written in seconds. For example, to set the cache equal to one hour, set the max-age equal to 3600 (the number of seconds in an hour).
Note
The META element is a little peculiar in that it does not require an ending tag. Instead, the META tag uses the following structure:
<meta/>
You won't use the HEAD and META elements very much throughout this book. However, they are important elements, especially when developing wireless applications where security and access are necessary.