6
Rest of WML
There is so much to WML,
that it is sometimes difficult to put it all together in some semblance of
order. We have covered all the major bits that you need to know to get started.
But there are other bitsy pieces that you could make use of. Often, it is the
small bits that few pay attention to that could really save the day. People are
usually lazy when it comes to optional items of use. But then, as always, one
has enough time to regret at a later stage.
In all programming
languages, there are these small insignificant bits that could be used, but are
not till it is too late. WML has plenty of these minor things that we couldn't
structure out in the right way. So we decided to put them in a chapter.
Comments are statements
that every programmer should make a good deal off. They are basically
statements for other humans to read and understand, but to be ignored by the
environment completely. WML also has comments.
A comment starts with a
<!-- and ends in -- >.
<!-- This is a comment -->
You can't put two --
anywhere. These comments are only for the person who wrote it, or for someone
who reads the program later. They do not reach the browser at all. When you
look at an HTML file in a browser, you can actually see the source code.
However, this is not possible here. Comments are only to clarify or make some
notations in the program. It maybe a complex piece of code, or one's actions
may not be clear; such as the reason for initialising a variable, or setting
M=22. They do not get executed or even stored when compiled. In a later chapter
where we discuss the wml bytecode - binary representation of wml - you will see
that this is not stored at all. There is no mention of the comment anywhere.
Another bitsy item that is
important, is how WML treats emptiness. Anything that is empty is tossed aside.
Compactness is the name of this game. Since, every little bit of space counts,
anything that has no value or is empty is knocked off automatically. So if you
have 6 blank lines, they get knocked off. In the same way, if you have a
<p> and a </p> a paragraph tag which has nothing, they get knocked
off.
|
Screen 6. 1 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Card 1
</p>
<p></p><p></p>
<p>
Card 2
</p>
</card>
</wml>
This is where WML is more intelligent
than you. It refuses to display an extra blank line. Spaces within text are
removed but spaces within an attribute value are not removed.
Don't you just wish that
you could apply the WML compiler to the heads of a number of your friends?
Wouldn't it be great to get rid of all that emptiness that is there? The only
problem is that the compiler knows how to cut out, it does not yet know how to
fill stuff in that would make some sense. It would look strange to see eyes
balancing on a nose and mouth as the whole back of the head would be cut off.
|
Screen 6. 2 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="hi bye">
<p>
Card 1
</p>
</card>
</wml>
Closeness is something that
we grow up with, but over the years we as people learn to grow more distant.
WML fixes this as well. Try keeping a great big gap between any two pieces of
text or entities, and WML will bring them back together. There are some cases
where this is ignored. You cannot always change basic nature. Basic nature or
qualities or attributes stay the same. All you can do in these cases is to tell
the person and hope they change.
In the above example, the
hi and bye are seen together as they are now part of the title attribute of the
card.
If you remember we had said
that in an input, you can enter the name of a variable. If the variable exists,
then the value is not seen. However, if the variable is unset, then the value
is displayed. In this case, the variable aa is defined using setvar to a pair
of double quotes. As the value is nblank space, the variable is said to be
unset and because it is unset, you will see the default value of AB.
|
Screen 6. 3 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card newcontext="true">
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value=" "/>
</refresh>
</onevent>
<p>
Card 1 $aa hi
<input name="aa" value="AB"/>
</p>
</card>
</wml>
Whether you give a single
space or double in the variable definition, a value gets assigned to the
variable. And hence, the new value which is entered is not seen. This is how
you can unset the variable. In other programming languages, one has the option
of defining the type of variable and creating it for use, like char, int, long
etc. Here, variables do not have any types.
Either the variable exists or it does not. And there is no other way
than using setvar, input, select, to change the value of a variable, as far as
we know.
But variable names are not
the only problem. Another problem that you will face is with the < and >
signs. These are reserved in XML/WML. There are a total of seven signs that
have special meaning in WML.
|
Screen 6. 4 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<p>
You &
</p>
</card>
</wml>
The old ampersand is first.
In the example we have used & Anything that begins with an & and
ends with a ; is called a named character entity. Whenever the sign is used, it
signals to the browser that the next character is a substitution. This must be
a number that can be translated to a character. You obviously understand that
this option is perfect to place characters that are not usually found on the
keyboard, yet a part of the character set.
Computers don't understand letters of the alphabet. They understand numbers. A, for example, is known as 65; a is 97; B is 66; b is 98.... while 0 is a 48. Hence, if you use A, you will see an A. x defines a hexadecimal number follows. Hence, B0 is 65 in hex, ° - A. The complete character set is defined by a standards body so there is no confusion in what is character has what value. This is called the ASCII code.
|
Screen 6. 5 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" >
<p>
You ° A
</p>
</card>
</wml>
You can use the &
anywhere. It could even be used in the title.
|
Screen 6. 6 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='y"es&' id="a1" >
</card>
</wml>
The title of a card is defined in such a way
that you can put anything in the title. Values can be put it in single or double
quotes. With the id attribute unfortunately, this cannot be done. id takes only
letters, numbers and 4 special characters :, ., - and _. There are certain rules that govern the
definition of the id and title. The rules are different for each of them.
XML/WML is case sensitive, lower case is different from upper case
- both for the letter and the numbers. ;)
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Card 1
</p>
</Card>
</wml>
|
Screen 6. 7 |
If you key in and execute
the above program exactly as it is shown, you will see an error. The capital C
in card caused calamities, 'cos case sensitive compilers cannot condone capital
characters.
The text that you use is
not case sensitive. After all, the text could be anything that you want. Only
some of the non-alphabetic and non-numeric characters need special attention.
Understand that the definitions in XML carry forward their rules to WML. This
proves that if card is defined with a small c in the DTD, it has to be used
with a small c in the WML file as well.
The DTD defines a limited number of attributes. You
cannot have more than that. Here the attribute name is mode and not modes.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p modes="nowrap">
Card 1
</p>
</card>
</wml>
|
Screen 6. 8 |
With computer languages,
the rules either apply, or they don’t. There is no in-between case. If
attributes are case sensitive, then all words that belong to the attribute
category are case sensitive. Hence you can't have a capital M for mode.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p Mode="nowrap">
Card 1
</p>
</card>
</wml>
|
Screen 6. 9 |
Try it and you will get an error. mode can have two values, either
wrap or nowrap. But if you use nowraps - s added, the microbrowser will display
an error box. If it doesn't, then it surely is not following the rules of XML.
In our case it gives an error as displayed below.
|
Screen 6. 10 |
This example is to check
the case sensitivity of attribute values. You get an error because the nowrap
can't start with a capital N.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p mode="Nowrap">
Card 1
</p>
</card>
</wml>
The next example displays images for you on the mobile screen
|
Screen 6. 11 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' >
<p>
Card 1
<img src="sunny.wbmp" alt="hi"/>
</p>
</card>
</wml>
With the img tag, you give
an src. src is initialised to the name of the picture. In the Nokia samples we
have one picture file called sunny.wbmp. Similar to a bmp file, a wbmp file has
a certain format, wbmp stands for wireless bitmap file. Fortunately here you
have sunny.wbmp, so you can see the picture. It is a very small picture. If you
don't have the picture, use the alt attribute.
|
Screen 6. 12 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' >
<p>
Card 1
<img src="rainy1.wbmp" alt="hi" localsrc="sunny.wbmp"/>
</p>
</card>
</wml>
In this example we have a
localsrc initialised to sunny,bmp whereas src is given rainy.wbmp. Since src
file is not present in the current directory, the alt value is displayed. The
order of display is first src, then alt and finally localsrc.
Every card is identified by
its id. You can have as many cards you want provided they have different ids.
The id of a card is unique. Cards are not the only elements in WML that can
have ids, paragraphs too can have ids.
All wml tags can have ids, and all these have to be unique. It is not possible
to have card id equalto a1 and a paragraph id equal to a1 as well.
Now is the time to
introduce a new tag ie anchor
|
Screen 6. 13 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Hello
<anchor title="bye">
hi
<prev />
</anchor>
</p>
</card>
</wml>
anchor is given a title of
bye and hi the text placed before </anchor>. Hi will be underlined when
displayed. The user interface will be similar to what we have seen earlier. No
difference whatsoever. An anchor, as the name suggests binds something. In this
case, to this anchor point we want to link to another site or execute a
routine. You can call it a link as it provides a link between this point the
destination we want to reach. The anchor text is clickable, and if we do click
on it, in this case, nothing happens.
Within the anchor tags, you
need a go. Earlier go href was an attribute; here it is an actual tag. It is a
tag, element and a task. Using go, we can now initialise variables. Hence the
anchor is more productive than the <a> tag.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Hello
<anchor title="bye">
<go href="#a1"/>
hi
</anchor>
</p>
</card>
<card id="a1">
<p>
He
</p>
</card>
</wml>
|
|
|
Screen 6. 14 |
Screen 6. 15 |
Screen 6. 16 |
|
Screen 6. 17 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Hello
<anchor title="bye">
hi <br/> bye
<go href="#a1"/>
</anchor>
</p>
</card>
<card id="a1">
<p>
He
</p>
</card>
</wml>
Using anchor, we can display images. Rather than just have text
that the user must click on to get ahead, one can click on a picture and get to
the link. Pictures do make things so much fun. After all, everyone knows that a
picture is worth a 1000 words.
|
Screen 6. 18 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Hello
<anchor title="bye">
hi <img src="cloudy.wbmp" alt="pic"/>
<go href="#a1"/>
</anchor>
</p>
</card>
<card id="a1">
<p>
He
</p>
</card>
</wml>
Try and spot the difference
between the use of the do and the anchor. The two examples make use of two
different methods to get our work done.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<do type="accept" label="no">
<go href="#a2"/>
</do>
<p>
Hello
<anchor title="bye">
hi <img src="cloudy.wbmp" alt="pic"/>
<go href="#a1"/>
</anchor>
</p> </card>
<card id="a1">
<p>
He
</p></card>
<card id="a2">
<p>
End
</p>
</card>
</wml>
|
|
|
|
Screen 6. 19 |
Screen 6. 20 |
Screen 6. 21 |
Screen 6. 22 |
Do and anchor, they both display
the same user interface. The user will not notice anything different. You could
use an <anchor>, an <a> or a <do>. The user will not notice
any difference at all. The <do>
is cumulative whereas the others are not.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<do type="accept" label="no">
<go href="#a1"/>
</do>
<p>
Hello
<anchor title="bye">
hi
<go href="#a1"/>
</anchor>
<a href="#a2">Good </a>
</p>
</card>
<card id="a1">
<p>
He
</p>
</card>
</wml>
|
|
|
Screen 6. 23 |
Screen 6. 24 |
Screen 6. 25 |
|
Screen 6. 26 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<do type="sss">
<go href="#a1" />
</do>
<p>
Hello
<a href="#a2" title="hh" >Good </a> <br/>
<a href="#a2">Bad </a>
<input name="aa"/>
</p> </card>
<card id="a1">
<p>
He
</p>
</card>
</wml>
|
|
|
|
Screen 6. 27 |
Screen 6. 28 |
Screen 6. 29 |
Screen 6. 30 |
|
Screen 6. 31 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Title">
<do type="aaa" name="a1">
<go href="#c1"/>
</do>
<do type="aaa" name="b1">
<go href="#c1"/>
</do>
<p>
Hello World!!!
</p>
</card>
</wml>
The type for both the do
tags is the same whereas the name is
different. Change the name to a1 in the second case and it will show you an
error message. As ids have to be unique per card, names too have to be unique
for every do.
What do parameters mean?
Where do variables fit in?
The answer lies in
understanding a little bit of human nature. Never accept to do more than you
should, there is always a chance that you may fail in the attempt. Hence, as
far as possible never overcommit yourself. If you commit yourself then you will
actually have to do it. To start with, a variable is one level of abstraction.
Suppose you want to write Hello World. The simplest way to do this and to save
lines and lines of code is to enter this exactly as it is - Hello World.
Another more elegant way of doing the same is by using a variable. For example,
we used the variable aa in onenterforward, and
set it to Hello World.
|
Screen 6. 32 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Title">
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="Hello World"/>
</refresh>
</onevent>
<p>
$aa
</p>
</card>
</wml>
Here is level one of abstraction.
While there appears to be no practical use of doing this, in the broader sense,
we have reduced a bit of our work by automating a function. Now we can control
it from one point. A good example would be displaying the company name on every
screen. Another benefit is that should
a change in the name be required, it only has to be done once and it will stay
in effect all through.
You can use variables
within tags which remain consistent throughout. The second place where you can
use variables is in the values of attributes. For example, when you have a
<do> and a </do>, there is a <go href>. Instead of hard
coding and stating the reference or the card to go to, you could use a variable
instead. This is shown in the next example where we use $aa inplace of a2
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Title">
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="a2"/>
</refresh>
</onevent>
<do type="zz">
<go href="#$aa"/>
</do>
<p>
$aa
</p></card>
<card id="a2" title="Title">
<p>
card 2
</p>
</card>
</wml>
|
|
|
Screen 6. 33 |
Screen 6. 34 |
Screen 6. 35 |
aa is set to a2 hence you move to a2. And this
is not the only place where you can use variables. The next chapter will reveal
more of this too you. But for now, you must understand that variable allow you
to substitute names so that you can make minor modifications that have major
consequences.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="#$aa">
<p>
</p>
</card>
</wml>
|
Screen 6. 36 |
This helps you learn the
workings of WML. Of course, you have been learning it all the way, but this is
a quick and easy explanation.
|
Screen 6. 37 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="$aa">
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="bbbb"/>
</refresh>
</onevent>
<p>
$aa
</p>
</card>
</wml>
Onenterforward will refresh
the screen and reset aa to bbbb. The title automatically takes the new value
and becomes bbbb. In the next program
the href attribute in go is initialized to bbbb which does not exis. Hence we
see invalid address as the error on the screen.
|
Screen 6. 38 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="$aa">
<onevent type="onenterforward">
<go href="#$aa">
<setvar name="aa" value="bbbb"/>
</go>
</onevent>
<p>
$aa
</p>
</card>
</wml>
The next example
demonstrates that the newcontext feature works for the browser session. This
means you create a variable i.e aa with the value bbbb and move on to another wml file using onenterforward, the
variable aa in the second wml file also gets the value bbbb.
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="$aa" newcontext="true">
<onevent type="onenterforward">
<go href="a33.wml">
<setvar name="aa" value="bbbb"/>
</go>
</onevent>
<p>
$aa
</p>
</card>
</wml>
|
Screen 6. 39 |
a33.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
Hi $aa
</p>
</card>
</wml>
a33.wml will print the new
value of aa as bbbb.
Here is an example using ontimer with variables.
|
Screen 6. 40 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title='yes' id="a1" ontimer="#$aa" >
<onevent type="onenterforward">
<refresh>
<setvar name="aa" value="a2"/>
</refresh>
</onevent>
<timer value="10"/>
<p>
Card 1
</p>
</card>
<card title='yes' id="a2" >
<p>
Card 2
</p>
</card>
</wml>
First the browser will
display Card 1and within a nanosecond
it changes to Card 2
Variables are used to avoid
redundancy and repetition. They are used for parameterisation You can change the value of variable depending upon the user input and
the effect or the result will vary.
The last example in this chapter has a new tag CDATA within square brackets. The opening bracket is preceded by an exclamation. Any tag which is not to be evaluated can be placed here. Hence <b> no more stands for bold and gets displayed as is. CDATA is termed as a section.
|
Screen 6. 41 |
a11.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card >
<p>
<![CDATA[ this is <b> a test]]>
</p>
</card>
</wml>
As of now this chapter is
left incomplete because we are not really convinced with the importance of
variables. Refer to the next section on
wmlscript where variables have been used heavily. Wmlscript is a programming language and with this programming
language you can do a lot more with variables than you have seen so far.
All the programs that we
have shown you till this point, has information or data flowing from one side.
To make the micro browser display a page in certain way, we use the WML
tags. But to make it a really useful
tool, you need to have a method of sending information back. When you key in
something, or select something from the list of options, you want this
information to go to a computer program which will process this data. This
program is called a gateway server. It could also be a web server on the
Internet. There is a certain way by which what you key in goes to the other
end.There are tags like postfield, access, head and meta which are used for
this two way communication.
WML deck and cards are understood by the micro
browser. Information may keep changing,
so if a certain WML file comes across to you - let's say that it has a certain
share price - the question asked is, do you want to see the page from the micro
browser's cache or, do you want to fetch it again from the wireless Internet?
There is caching in the browser. How
often do you go back? How often do you see contents from your cache ? When does
the cache get reset, is it after 3 days or 30 minutes?
This is only the beginning. There are still many other little bits that one can use. We have only covered some of the more important bits and pieces that we felt you needed to know before you get ahead. Read on, there are just a couple of chapters before you end this section.