If-Then-Else Logic Part 1
 
    Understanding RXML Syntax for If-Then-Else Logic Part 1
By Joe Follansbee, joef@follansbeeconsulting.com
Introduction
Virtually every programming language uses conditional statements. In a program, these statements instruct the computer to do something if some test is met. Although Roxen eXtensible Markup Language (RXML) is not a programming or scripting language per se, it contains features that make Web pages containing RXML tags behave like simple programs. It's therefore important that Caudium Web developers understand how these features work. Part 1 of this two part tutorial takes you through the basic of RXML's if-then-else logic. Part 2 looks at more advanced features.
Assumptions
This tutorial assumes the reader has a basic understanding of programming and markup languages, such as HTML. However, I've written the document so that a beginning developer can copy and paste code that will work immediately.
Requirements
  • Caudium 1.0.34 (or later)
  • Access to a Caudium virtual server file system (i.e., a directory where Web pages are stored)
  • A text editor
I suggest using the examples in this tutorial to practice using the RXML tags discussed. To test your code, simply create an HTML file containing the tags and load the page in your browser. Caudium or the browser will often tell you when something is wrong.
A Mini-Tutorial
Before discussing RXML's if-then-else syntax, let's briefly review conditional statements. Computer programmers routinely use "if" statements to check if something is true or false. For example, a programmer may ask himself, "Am I hungry?" Sometimes, the statement is true, or more technically, the statement returns a value of "true".

The programmer will use this value to tell his brain to do something. "If I am hungry," he tells himself, "I will eat pizza." Here's another way to write it:
if (hunger is true) then { eat pizza }
Conversely, the programmer could tell himself NOT to do something, depending on the same variable, "hunger."
if (hunger is false, i.e., "I'm not hungry") then { do not eat pizza }
Taking this one step further, the programmer can give himself an option, based on whether the variable is true or false. "If I'm not hungry," he says, "I won't eat pizza, but I will drink coffee." Put another way,
if (hunger is true) then { eat pizza } else { drink coffee }
In fact, most of today's programming languages actually use the words "if" and "else" to clearly state tests or conditions. However, the word "then" is almost always implied, rather than used explicitly. RXML follows this standard way of speaking programmatically.
The <if> Tag
In RXML, tags written into an HTML page tell the Caudium Web server to perform some action when the page is requested by a Web browser. For example, the <date> tag tells Caudium to display the current date. By giving the tag attributes, Caudium will modify the tag's result. Writing <date type="iso"> displays the date as YYYY-MM-DD. By giving the <if> tag various attributes, you can fine tune the behavior of your Web page.

The <if> tag is a "container" tag, meaning it has an open <if> tag and a closing </if> tag. Any of a number of things can go inside the container: logic to set variables, HTML code, even other <if> tags. But there is always an opening and a closing, so that the tag always looks like this:

<if attribute=something>some action</if> (Note the forward slash in the closing </if> tag.)
<if> and Variables
The <if> tag is almost always associated with a variable of some kind. We'll use it to examine the values of variables your users might set in a Web form to tell Caudium to display an order for a meal. Imagine a Web form with two checkboxes:

<FORM ACTION="showorder.html">
<B>Make your selections:</B><BR>
<INPUT TYPE="checkbox" NAME="pizza" VALUE="true">Pizza
<INPUT TYPE="checkbox" NAME="coffee" VALUE="true">Coffee
<INPUT TYPE="submit">
</FORM>


When the user clicks the Submit button, he sends the values of the checkboxes named "pizza" and "coffee" to the Caudium Web server via the page "showorder.html". In showorder.html, you've included these <if> tags:

<HTML>
<HEAD><TITLE>If-Then-Else Tutorial Script</TITLE></HEAD>
<BODY>

(This RXML tag will tell you if Caudium finds certain problems.)
<debug>

(If the user checked "pizza", tell him so.)
<if variable="pizza is true">You ordered pizza.</if><BR>

(If the user checked "coffee", tell him so.)
<if variable="coffee is true">You ordered coffee.</if>

</FORM>
</BODY>
</HTML>
<elseif>
Here's a slight variation on the above code that uses RXML's <elseif> tag.

<if variable="pizza is true">You ordered pizza.</if>
<elseif variable="coffee is true">You ordered coffee.</elseif>


Unlike an ordinary <if> statement, an <elseif> statement will only evaluate if the preceding <if> statement is false.
<else>
You could give the user more detailed feedback by writing the statements with <else> tags:

<if variable="pizza is true">You ordered pizza.</if>
<else>You didn't order pizza. Are you sure?</else>

<if variable="coffee is true">You ordered coffee.</if>
<else>You didn't order coffee. Are you sure?</else>
Logical Operations
Often times, programmers need to check the values of two or more statements before performing an action. In most kinds of programming, the logical operators "AND", "OR", and "NOT" help programmers perform these multi-level tests. Our hungry programmer could think, "If I eat a pizza and drink coffee, I'll take a bicarbonate of soda." Put another way,
If ((pizza is true) AND (coffee is true)) { take a bicarb}
Unfortunately, in the current state of RXML development, Web developers can't rely on using an explicit "AND" statement to test the truth or falsehood of two or more related <if> statements. The "AND" has to be implied with nested <if> or <else> statements. Here's a fairly complex example,

<if variable="pizza is true">
    <if variable="coffee is true">
    You ordered pizza and coffee.
    </if>
    <else>
        You ordered only pizza.
    </else>
</if>
<else>
    <if variable="coffee is true">
        You ordered only coffee.
    </if>
    <else>
        Please order pizza AND coffee.
    </else>
</else>


Here's another way to use "AND" logic, slightly more advanced. The <formoutput> tags make use of the values from the Web form. The words between the "#" characters are the names of the INPUT elements from the Web form.

<formoutput>
    <if match="#pizza#, #coffee# is true, true">
        You ordered pizza and coffee.
    </if>
    <else>
        Please order pizza AND coffee.
    </else>
</formoutput>


A similar situation exists for "OR" tests. Here's one of the "AND" examples modified as an example of "OR". The code asks whether the user wants pizza OR coffee. If neither item is checked, the <else> statement is displayed. Note the <elseif> statement.

<if variable="pizza is true">
    You ordered pizza.
</if>
<elseif variable="coffee is true">
    You ordered coffee.
</elseif>
<else>
    You ordered neither pizza nor coffee.
</else>

Another way of testing the truth of a statement is by asking the question in the negative. Assuming pizza and coffee are the only choices, and the user must choose one or both, the statement "You ordered pizza" means the same thing as "You did not order coffee." In RXML, you could write a test this way:

<if not variable="pizza is true">You did not order pizza.</if>
<else>You ordered pizza</else>


As you might guess, this kind of logic can be easily confusing. However, since "NOT" is the only logical operator that can be stated explicitly in a reliable way, using this syntax might be useful.
Conclusion
If-Then-Else logic is a basic part of any scripting or programming language. RXML includes this kind of logic, although the syntax can be confusing at first. But once you've written two or three Web pages with RXML tags, you'll easily master the fundamentals.

Copyright 2002, Joseph G. Follansbee. The HTML and RXML code in this document is released under terms of the GPL. Thanks to Thomas Koester and Marek Habersack for code suggestions.
 
HTML OK CSS